SQLite3中的内部联接

时间:2017-07-07 01:44:13

标签: sql sqlite inner-join

我试图在SQLite3中执行内连接并且它无法正常工作。我不断收到错误消息,指出table2中的字段名称不明确。

感谢任何关于原因的输入。

SELECT 
table1.id, table1.cd1, table1.cd2, table1.cd3, table1.cd4, table2.ab

FROM 
table1

INNER JOIN table2 ON table2.dx = table1.cd1 
INNER JOIN table2 ON table2.dx = table1.cd2
INNER JOIN table2 ON table2.dx = table1.cd3
INNER JOIN table2 ON table2.dx = table1.cd4

GROUP BY 1, 2, 3, 4, 5, 6

字段" dx"在table2中对应于table1中的四个不同字段:" cd1"," cd2"," cd3"," cd4。"

1 个答案:

答案 0 :(得分:2)

您需要表别名:

SELECT t1.id, t1.cd1, t1.cd2, t1.cd3, t1.cd4, ?.ab
FROM table1 t1 JOIN
     table2 t21
     ON t21.dx = t1.cd1 JOIN
     table2 t22
     ON t22.dx = t1.cd2 JOIN
     table2 t23
     ON t23.dx = t1.cd3 JOIN
     table2 t24
     ON t24.dx = t1.cd4 
GROUP BY 1, 2, 3, 4, 5, 6;

我不知道哪个表ab应该来自哪个,所以你需要填写它。