2012-07-17 1 views
0

내 DB에 다음 표가 오전에 의해 2 테이블과 조인 순서를MySQL은</p> <p>프로젝트 수를

+----+-------------------------------------------+ 
| 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 | 
+-------+-----------+-------------+ 

프로퍼티 (where property.id = project_assignment.property_id)

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

내가 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

시도해 보셨습니까? 우리는 일반적으로 당신을 돕기 위해 일하는 것이 아니라 돕고 싶습니다. –

+0

나는 이것을 시도했다 –

+0

"this"는 무엇인가? 시도한 SQL 쿼리가 표시되지 않습니다. –

답변

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; 
관련 문제