【问题标题】:cakephp / php Mysql complex query for dashboard statisticscakephp / php Mysql 复杂查询仪表盘统计
【发布时间】:2012-08-31 07:24:46
【问题描述】:

我有以下 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 作为外键

任何想法,相同的查询是什么?

谢谢!!

【问题讨论】:

    标签: php statistics dashboard


    【解决方案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 ;
    

    希望它可以帮助某人满足他们的要求!

    谢谢

    【讨论】:

      【解决方案2】:

      我已经准备了一个查询更改是根据你的表格字段:

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

      【讨论】:

      • 感谢您的快速响应,它适用于国家/地区但是我如何修改它以适应州/城市组合?请告诉我..
      • tnx 但我不能从单个查询中管理所有内容,就像在 cakephp 中一样,管理所有的差异查询并不困难
      • 那么你应该按照给定的链接并在关系中添加countercache
      猜你喜欢
      • 2018-07-07
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多