SELECT
Tickets.id,
(SELECT Users.Name FROM Users INNER JOIN Tickets ON Tickets.Creator = Users.id) AS naam,
(SELECT Users.Name FROM Users INNER JOIN Tickets ON Tickets.Owner = Users.id) AS name
FROM Tickets
WHERE Tickets.id = 64;
我想要一行
id, username(Tickets.creator= Users.id), username(where Tickets.Owner = Users.id)
Users
id | name
----------
1 | jan
2 | henk
3 | maria
tickets
ticketsid | owner | creator
-------------------------------
1 | 3 | 2
2 | 1 | 3
3 | 2 | 3
答案 0 :(得分:1)
您的查询希望与外部查询相关联。你错过了通过对tickets
进行额外加入:
SELECT
Tickets.id,
(SELECT Users.Name FROM Users WHERE Tickets.Creator = Users.id) AS naam,
(SELECT Users.Name FROM Users WHERE Tickets.Owner = Users.id) AS name
FROM Tickets
WHERE Tickets.id = 64;
您还可以使用join
s表达此查询:
select t.id, uc.name as CreatorName, uo.name as OwnerName
from Tickets t left outer join
Users uc
on t.Creator = uc.id left outer join
Users uo
on t.Owner = uo.id
where t.id = 64;
这是表达此查询的更典型方式。
答案 1 :(得分:0)
怎么样
select t.id, o.name, c.name
from tickets t
join users o on o.id = t.owner
join users c on c.id = t.creator
where t.id = 64;