我是MySQL的初学者。
表1:用户
ID NAME
----------
1 XYZ
2 ABC
表2:任务
ID TASK_NAME ASSIGNED_BY ASSIGNED_TO
--------------------------------------------
1 task 1 1 2
2 task 2 2 1
我想要这个结果。
ID TASK_NAME ASSIGNED_BY ASSIGNED_TO
--------------------------------------------
1 task 1 XYZ ABC
2 task 2 ABC XYZ
答案 0 :(得分:1)
这是一个简单的JOIN
任务:
SELECT t.ID, t.TASK_NAME,
u1.NAME AS ASSIGNED_BY,
u2.NAME AS ASSIGNED_TO
FROM Task AS t
JOIN Users AS u1 ON t.ASSIGNED_BY = u1.ID
JOIN Users AS u2 ON t.ASSIGNED_TO = u2.ID
答案 1 :(得分:0)
您需要使用INNER JOIN
来执行此操作。我使用SELECT t.ID, t.TASK_NAME,
u1.NAME AS ASSIGNED_BY,
u2.NAME AS ASSIGNED_TO
FROM Task AS t
INNER JOIN Users AS u1 ON t.ASSIGNED_BY = u1.ID
INNER JOIN Users AS u2 ON t.ASSIGNED_TO = u2.ID
请尝试以下查询。
parfor