简体   繁体   English

4表深度mysql查找

[英]4 Table depth mysql lookup

I have 4 tables - companies, projects, workers, tasks Each company has several projects, Each projects has several workers, Each worker has several tasks. 我有4个表格-公司,项目,工人,任务每个公司有几个项目,每个项目有几个工人,每个工人有多个任务。

In the tasks table, there is an "estimated_cost" field. 在任务表中,有一个“ estimated_cost”字段。 I want to view the list of all the companies, but I want it sorted by the estimated_cost of the task with the highest estimated_cost within it. 我想查看所有公司的列表,但我希望按照任务中的估计费用最高的任务的估计费用进行排序。

Any ideas? 有任何想法吗?

I use MSSQL not MySQL so the syntax may not be 100%... but try this. 我使用的是MSSQL,而不是MySQL,因此语法可能不是100%...但是请尝试此操作。

SELECT company.name, MAX(estimated_cost) AS HighestCost
FROM companies 
INNER JOIN projects ON projects.companyid = projects.id
INNER JOIN workers ON workers.projectid = workers.id
INNER JOIN tasks ON tasks.workerid = tasks.id
GROUP BY company.name
ORDER BY HighestCost DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM