嗨,我试着用两个内部连接进行查询,我的错在哪里?
选择job_tbl.id,accounts.username作为起始,accounts.username作为工作者,job_tbl.comment,job_tbl.date,job_tbl.status
来自工作
job上的内部联接帐户\u tbl.starter=accounts.id
job上的内部联接帐户\u tbl.worker=accounts.id
工作表如下:
+----+---------+--------+---------+------+--------+
| id | starter | worker | comment | date | status |
+----+---------+--------+---------+------+--------+
| 1 | 1 | 3 | qwe | date | 10 |
+----+---------+--------+---------+------+--------+
| 2 | 2 | 1 | qwe | date | 10 |
+----+---------+--------+---------+------+--------+
此处显示帐户表:
+----+------------+-----------+-------+
| id | username | extension | email |
+----+------------+-----------+-------+
| 1 | Julia | 100 | email |
+----+------------+-----------+-------+
| 2 | Eve | 101 | email |
+----+------------+-----------+-------+
| 3 | Max | 102 | email |
+----+------------+-----------+-------+
结果是:
+----+---------+--------+---------+------+--------+
| id | starter | worker | comment | date | status |
+----+---------+--------+---------+------+--------+
| 1 | Julia | Max | qwe | date | 10 |
+----+---------+--------+---------+------+--------+
| 2 | Eve | Julia | qwe | date | 10 |
+----+---------+--------+---------+------+--------+
最佳答案
问题是连接两个表时未在表上指定ALIAS
,导致状态不明确。
SELECT a.*,
b.username StarterName,
c.userName WorkerName
FROM job_tbl a
INNER JOIN account b
ON a.starter = b.id
INNER JOIN account c
ON a.worker = c.ID
若要进一步了解有关联接的更多信息,请访问以下链接:
Visual Representation of SQL Joins
关于mysql - MySQL,第二个INNER JOIN,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15408191/