sql - SQLite3 中的内部连接

标签 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

表2中的字段“dx”对应表1中的四个不同字段:“cd1”、“cd2”、“cd3”、“cd4”。

最佳答案

您需要表别名:

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 应该来自哪个表,所以你需要填写它。

关于sql - SQLite3 中的内部连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44961306/

相关文章:

SQL Server 2008 - 对于 XML 路径非常慢

mysql - 出现错误 - 每个派生表都必须有自己的别名错误

SQL Server 数据库更改工作流最佳实践

android - SQLiteReadOnlyDatabaseException : attempt to write a readonly database (code 1032)

mysql - 有没有办法在同一列上进行 SELF JOIN?

c# - 如何在关系或预取上执行联接或放置 where 子句? (LLBLGen)

mysql - 如何根据另一个表的选择在表的列中插入值

java - 更新语法错误

xcode - 从sqlite获取日期时需要帮助

java - 如何在使用 SQLite 操作期间添加行?