2012-09-06 126 views
0

我有以下MySQL數據庫表:的CakePHP/PHP的MySQL的複雜查詢

cities

states

countries

members

samajs(一羣人國際)

,我要創建我dashboard網頁查詢,這將有如下結果:

Country Members Samajs Total (table header) 

Country1 7  5  16  (country row with total members, samajs and total count) 

    state1 5  2  7  (state row with total members, samajs and total count) 

    city1 3  1  4  (all cities in that state, city row with total members, samajs and total count) 

    city2 2  0  2   

    state2 2  1  3 

Country2 3  2  5  (country row with total members, samajs and total count) 

...and vice versa.... 

這裏,members表將有country_id, state_id and city_id爲外鍵

samajs表還會有country_id, state_id and city_id作爲外鍵

任何想法,將會查詢什麼一樣的嗎?

謝謝!

回答

0

我已經準備根據你的表字段作爲查詢條件的變化:

// for country based 
SELECT 
countries.name AS countryName, 
(SELECT 
count('x') FROM members WHERE members.country_id = countries.id) as totalMembers, 
COUNT(samajs.country_id) as totalSamajs 
FROM 
    `countries` 
INNER JOIN samajs ON samajs.country_id = countries.id 
GROUP BY 
    countries.name 
ORDER BY 
    totalMembers DESC 

// state based result 

    SELECT 
states.name AS stateName, 
(SELECT 
count('x') FROM members WHERE members.state_id = states.id) as totalMembers, 
COUNT(samajs.state_id) as totalSamajs 
FROM 
    `states` 
INNER JOIN samajs ON samajs.state_id = states.id 
GROUP BY 
    states.name 
ORDER BY 
    totalMembers DESC 

//city based result 

SELECT 
cities.name AS cityName, 
(SELECT 
count('x') FROM members WHERE members.city_id = cities.id) as totalMembers, 
COUNT(samajs.city_id) as totalSamajs 
FROM 
    `cities` 
INNER JOIN samajs ON samajs.city_id = cities.id 
GROUP BY 
    cities.name 
ORDER BY 
    totalMembers DESC 

,並按照CakePHP的方法,你可以遵循這樣一個問題:
Order data based on count of related table data

+0

感謝您的快速反應,它適用於國家,但我怎麼能修改它的狀態/城市組合?請讓我知道.. –

+0

已編輯我的答案可以使用它 – Krishna

+0

tnx但我不能管理所有的事情從單一查詢,如在cakephp這將是很難管理差異查詢所有 –

1

最後,由單一的查詢,這使我預期的結果在unionsubqueries的幫助下如下:

SELECT country_id, state_id, city_id, country, membercount, samajcount FROM 
     (

     SELECT con.country_id, -1 as state_id, -2 as city_id, con.country, 
     (SELECT COUNT(member_id) FROM members WHERE country_id = con.country_id) as membercount, 
     (SELECT COUNT(samaj_id) FROM samajs WHERE country_id = con.country_id) as samajcount 
     FROM countries as con 
     group by con.country 

     UNION 

     SELECT s.country_id, s.state_id, -2 as city_id, s.state as country, 
     (SELECT COUNT(member_id) FROM members WHERE state_id = s.state_id) as membercount, 
     (SELECT COUNT(samaj_id) FROM samajs WHERE state_id = s.state_id) as samajcount 
     FROM states as s 
     group by s.state 

     UNION 

     SELECT c.country_id, c.state_id, c.city_id, c.city as country, 
     (SELECT COUNT(member_id) FROM members WHERE city_id = c.city_id) as membercount, 
     (SELECT COUNT(samaj_id) FROM samajs WHERE city_id = c.city_id) as samajcount 
     FROM cities as c 
     group by c.city 

     ) COUNTRY 

     order by country_id, state_id, city_id, country ; 

希望它可以幫助他人滿足他們的要求!

謝謝