2012-07-17 30 views
0

我有在我的DB下表2加入的表和順序MySQL的內通過計數

項目

+----+-------------------------------------------+ 
| id | name          | 
+----+-------------------------------------------+ 
| 1 | YANNONALI COURT       | 
| 2 | UNIVERSITY OF COLORARDO DENVER RESEARCH 2 | 
| 3 | G.R.E.A.T PROGRAM DESALTER BUILDING  | 
| 4 | MONARCH CLUB        | 
| 5 | LAFAYETTE MERCANTILE      | 
| 6 | CAMELBACK VILLAGE RAQUET AND HEALTH CLUB | 
| 7 | BACK COUNTRY        | 
| 8 | URBAN CRASHPAD       | 
| 9 | PRIVATE RESIDENCE       | 
| 10 | EATON RESIDENCE       | 
+----+-------------------------------------------+ 

PROJECT_ASSIGNMENTS(WHERE projects.id = project_assignment.target_id)

+-------+-----------+-------------+ 
| id | target_id | property_id | 
+-------+-----------+-------------+ 
| 19178 |   1 |   48 | 
| 19192 |   1 |   39 | 
| 19391 |   1 |   3 | 
| 19412 |   2 |   3 | 
| 19591 |   2 |   34 | 
| 19610 |   2 |   34 | 
| 21013 |   3 |   2 | 
| 21032 |   3 |   2 | 
| 30876 |   4 |  2433 | 
| 38424 |   5 |  2580 | 
+-------+-----------+-------------+ 

PROPERTIES(WHERE properties.id = project_assignment.property_id)

+----+------------------+ 
| id | name    | 
+----+------------------+ 
| 2 | Residential  | 
| 3 | Multi Family  | 
| 34 | New Construction | 
| 39 | Contemporary  | 
| 48 | Southwest  | 
+----+------------------+ 

我想O/P在列表節數項目有序......

Residential(177) //12 - total no.of projects which is having this property 
Multi Family(15) 
New Construction(13) 
Contemporary(11) 

,請給我一些MySQL的查詢

     Thank You 
+6

你有試過什麼嗎?我們一般喜歡幫忙,而不是爲你做這項工作。 – 2012-07-17 07:19:01

+0

我試過這個 – 2012-07-17 07:29:48

+0

什麼是「這個」?我沒有看到您嘗試過的任何SQL查詢。 – 2012-07-17 21:57:25

回答

0

這應該做的伎倆:

select 
    c.name, 
    count(c.id) as CountOfProperties 
from 
    projects a, 
    project_assignments b, 
    properties c 
where 
    a.ID=b.target_id 
    and b.property_id=c.ID 
group by 
    c.name 
order by 
    count(c.id) desc; 
0

試試這個::

select 
    prop.name, 
    count(prop.id) as CountOfProperties 
from 
    projects p 
    inner join project_assignments pa on (p.ID=pa.target_id) 
    inner join properties prop on (pa.property_id=prop.ID) 
group by 
    prop.name 
order by 
    count(prop.id) desc;