2017-05-09 57 views
-1

我有6列查詢,我也想得到其他表列的總和。如何從子查詢中獲取一行?

我希望子查詢的結果在一行中,但它重複。

select t.id, t.subject, t.created_at, u.name, l.name as l_name, 
ty.name as t_name 
,(select sum(new) from statuses) as new 
,(select sum(open) from statuses) as open 
,(select sum(close) from statuses) as close 
,(select sum(`delete`) from statuses) as `delete` 
from tickets t 
left join users u on u.id=t.user_id 
left join levels l on l.id=t.level_id 
left join types ty on ty.id=t.type_id 

你能幫我嗎?

enter image description here

+1

所以應該是在所有其他行是什麼? – xQbert

回答

0

您可以使用在GROUP BY

select t.id, t.subject, t.created_at, u.name, l.name as l_name, 
ty.name as t_name 
,(select sum(new) from statuses) as new 
,(select sum(open) from statuses) as open 
,(select sum(close) from statuses) as close 
,(select sum(`delete`) from statuses) as `delete` 
from tickets t 
left join users u on u.id=t.user_id 
left join levels l on l.id=t.level_id 
left join types ty on ty.id=t.type_id GROUP BY u.name; 
+0

除非外部選擇中的所有內容都依賴於u.name,否則無效使用GROUP BY。 –