好,所以我要使用两个表格-项目和服务,因此简化了:
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)找到,而project_id =某物.
到目前为止,我有
SELECT project.name
FROM service
LEFT JOIN project ON project.id = service.project_id
LIMIT 100
但我不知道如何订购.
解决方法:
您需要一个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