好的,所以我有两张我正在使用的牌桌 - project
和service
,因此简化了:
project
-------
id PK
name str
service
-------
project_id FK for project
time_start int (timestamp)
time_stop int (timestamp)
一对多的关系。
现在,我想返回(最好是一个查询)一个任意数量项目的列表,按照花费在它们上面的总时间排序,这可以通过SUM(time_stop) - SUM(time_start) WHERE project_id =
找到。
到目前为止,我有
SELECT project.name
FROM service
LEFT JOIN project ON project.id = service.project_id
LIMIT 100
但我无法弄清楚ORDER BY
的内容。
答案 0 :(得分:4)
您需要GROUP BY:
SELECT project.name
FROM service
LEFT JOIN project ON project.id = service.project_id
GROUP BY project_id
ORDER BY SUM(time_stop - time_start)
LIMIT 100