我一直坚持这个。我有 3 个表,并希望将表 2 和 3 与不同列的表 1 匹配。
tasks:
id | item1_id | item2_id
--------------------------------------------
1 | 4 | 5
2 | 5 | 6
3 | 6 | 7
--------------------------------------------
item1:
id | item1_name
--------------------------------------------
4 | item1_a
5 | item1_b
6 | item1_c
--------------------------------------------
item2:
id | item2_name
--------------------------------------------
5 | item2_a
6 | item2_b
7 | item2_c
--------------------------------------------
我一直在尝试的是:
SELECT tasks.id AS taskID, item1.name AS item1Name, item2.name AS item2Name
FROM tasks LEFT JOIN (item1 CROSS JOIN item2)
ON (tasks.item1_id = item1.id AND tasks.item2_id = item2.id),
users, notes
WHERE users.task_id = tasks.id
AND notes.task_id = tasks.id;
我正在返回任务,但不是来自 item1 或 item2 的信息。