1

我有这个查询:

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是 a tinyintthat is 1or 0

添加到我的查询中,如果所有分组的列行都相等,如何使返回paid列显示,否则显示该列?1paid10paid

4

2 回答 2

5

我认为您可以使用 sql 中的min关键字。就像是:

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
于 2013-04-26T21:25:17.080 回答
2

选择最低(付费)也许?如果有 0,则应为 0,否则应为 1。

于 2013-04-26T21:25:59.517 回答