我有这样的数据库:
Table1:
----------
id
id_item
tablename (enum: 'table2','table3','table4')
table2:
----------
id
value
table3:
-----------
id
value
table4: [...]
我想要一个类似的查询:
SELECT t1.id, t2.value FROM table1 AS t1
LEFT JOIN t1.tablename as t2 ON t1.id_item=t2.id
我试过这个“ERROR 1146(42S02):表't1.table'不存在”
请任何人建议类似于此或新格式的查询我正在使用
提前感谢。
答案 0 :(得分:0)
SELECT t1.id, t2.value FROM table1 AS t1 LEFT JOIN t1.tablename as t2 ON t1.id_item=t2.id
为什么要使用第一个表别名加入表名 tablename ?
<强> t1.tablename 强>
将查询修改为
SELECT t1.id, t2.value FROM table1 AS t1 LEFT JOIN tablename as t2 ON t1.id_item=t2.id
^Here
答案 1 :(得分:0)
尝试此查询: -
SELECT t1.id, t2.value
FROM table1 t1
LEFT JOIN (SELECT id, tablename FROM table1) t2
ON t1.id_item=t2.id;
这可能是你所寻找的。 p>