MYSQL查询使用变量作为LEFT JOIN中的表名 [英] MYSQL query using variable as table name in LEFT JOIN

查看:326
本文介绍了MYSQL查询使用变量作为LEFT JOIN中的表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SELECT var1,var2,var3,table_name 
FROM table1 LEFT JOIN table_name on var3=table_name.id

意思是我想根据table1table_name的值来动态离开联接表,因为var3是从那里取来的.

Meaning I want to dynamically left join table, depending on value of table_name from table1, since var3 is taken from there.

但是上面的查询结果是

表table_name不存在

table table_name does not exist

我对mysql限制有误吗?

My mistake of mysql limitation?

推荐答案

表名和列名在SQL查询中不能是动态的.因此,您必须使用2个查询或存储过程以编程方式应用逻辑,请参见此处的示例:

Table names, as well as column names, can't be dynamic in an SQL query. So you have to apply your logic programmatically, using 2 queries, or with a stored procedure, see an example here: http://forums.mysql.com/read.php?98,126506,126598#msg-126598

这篇关于MYSQL查询使用变量作为LEFT JOIN中的表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆