五个Mysql表中每个国家,州,城市和地区的销售总额

时间:2018-09-01 11:26:05

标签: php mysql sql mysqli

我有五个下表,我想通过一个mysql查询显示每个国家,州,城市和地区的销售总额:

注意:如果州,城市,区中没有销售,那么我需要查询结果显示0或空白(即使州,城市,区名称没有销售,也必须显示该名称)特定的州,城市或地区)

+------------+----------+
|  country              |
+------------+----------+
| country_id | country  |
+------------+----------+
| 1          | country1 |
| 2          | country2 |
+------------+----------+


+----------------+--------+------------+
| state_province                       |
+----------------+--------+------------+
| state_id       | state  | country_id |
+----------------+--------+------------+
| 1              | state1 | 1          |
| 2              | state1 | 2          |
| 3              | state2 | 2          |
| 4              | state2 | 1          |
+----------------+--------+------------+


+---------+-------+----------+
|  city                      |
+---------+-------+----------+
| city_id | city  | state_id |
+---------+-------+----------+
| 1       | city1 | 1        |
| 2       | city2 | 1        |
| 3       | city1 | 3        |
| 4       | city2 | 3        |
| 5       | city1 | 4        |
| 6       | city2 | 4        |
+---------+-------+----------+

请注意,上表中国家(country_id = 2)的州(state_id = 2)没有城市。

+-------------+-----------+---------+
|  district                         |
+-------------+-----------+---------+
| district_id | district  | city_id |
+-------------+-----------+---------+
| 1           | district1 | 1       |
| 2           | district2 | 1       |
| 3           | district1 | 2       |
| 4           | district2 | 2       |
| 5           | district1 | 4       |
| 6           | district2 | 4       |
| 7           | district1 | 5       |
| 8           | district1 | 6       |
+-------------+-----------+---------+


+----------+------------+----------+---------+-------------+--------+
|  sales                                                            |
+----------+------------+----------+---------+-------------+--------+
| sales_id | country_id | state_id | city_id | district_id | amount |
+----------+------------+----------+---------+-------------+--------+
| 1        | 1          | 0        | 0       | 0           | 1000   |
| 2        | 1          | 0        | 0       | 0           | 2000   |
| 3        | 1          | 1        | 0       | 0           | 300    |
| 4        | 1          | 1        | 0       | 0           | 70     |
| 5        | 1          | 1        | 1       | 0           | 50     |
| 6        | 1          | 1        | 1       | 1           | 25     |
| 7        | 1          | 4        | 1       | 1           | 25     |
| 8        | 1          | 4        | 5       | 0           | 25     |
| 9        | 2          | 0        | 0       | 0           | 3000   |
| 10       | 2          | 0        | 0       | 0           | 500    |
| 11       | 2          | 3        | 0       | 0           | 300    |
| 12       | 2          | 3        | 4       | 6           | 70     |
+----------+------------+----------+---------+-------------+--------+

Demo with my current attempt

谢谢

1 个答案:

答案 0 :(得分:1)

如果要按三列(city_idstate_iddistrict_id)进行分组,那是没有意义的,因为它不会更改sales表中的任何内容。那里您已经共享了信息。

此外,在sales表中,您用0代替了某些ID,并且在任何表中都没有ID等于0。您需要重新考虑我真正想要的。

我认为您只需要简单的JOIN:

SELECT country,
       state,
       city,
       district,
       coalesce(amount, 0) amount
FROM country ctr
JOIN state_province st ON ctr.country_id = st.country_id
JOIN city c ON c.state_id = st.state_id
LEFT JOIN district d ON d.city_id = c.city_id
LEFT JOIN sales s ON
    s.country_id = ctr.country_id AND
    s.state_id = st.state_id AND
    s.city_id = c.city_id AND
    s.district_id = d.district_id

Demo