14
我有兩個表:如何使用postgresql進行排序?
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太:/
任何想法?
我的表實際上是要高於兩列(城市名)時,我應該GROUP BY ALL他們? – Ggolo 2009-09-09 23:53:37
我認爲你應該爲GROUP BY子句起作用,否則查詢將會失敗 – MaxiWheat 2009-09-10 00:19:39
@Ggolo:你選擇的任何列,你必須'group by'。 – Eric 2009-09-10 00:28:29