2013-04-26 83 views
1

我有這樣的查詢:顯示器1如果所有分組的行是1,否則顯示0

select *, sum(services.amount) as service_amount from invoices 
left join projects using(project_id) 
left join services using(invoice_id) 
where client_id = ? 
group by project_id 
order by issue_date desc 

在表invoices我有一個柱paid其是tinyint10

添加到查詢我有,我怎樣才能使返回paid列顯示1如果所有的分組paid列行等於1否則顯示0paid列?

回答

5

我想你可以使用SQL中的分鐘關鍵字。例如:

select MIN(paid), sum(services.amount) as service_amount from invoices 
left join projects using(project_id) 
left join services using(invoice_id) 
where client_id = ? 
group by project_id 
order by issue_date desc 
+0

我喜歡這個解決方案! – 2013-04-26 21:26:54

+0

歡迎您:d – 2013-04-26 21:46:56

2

選擇min(paid)也許?如果有0和1,則應該爲0。