我有两张桌子
Companies: (id,name,city) Workers: (id,name)
我希望得到所有的公司,并按照雇员人数排序.
结果应该是:
count | company id | company name | city ------------------------------------------ 90 6 foo corp NY 45 9 bar corp LA 0 3 foobar corp HO
我试过了:
select c.*,count(w.id) as c from companies c left join workers w on c.id = w.company_id group by c.id order by c desc;
但是,这不能正常工作,因为它告诉我通过g.name进行分组:/
有任何想法吗?
你将表和列别名为同样的事情,所以不要这样做.这不是无效的,只是很难遵循.
原文链接:https://www.f2er.com/postgresql/192693.html无论如何,您可以通过以下方式将您所选择的所有不是聚合的列:
select count(w.id) as mycount,w.company_id,c.company_name,c.city from companies c left join workers w on c.id=w.company_id group by w.company_id,c.city order by mycount desc;