我有这张表格,其中包含有关orgin,出生,公民身份和性别国家的数据的人。
PEOPLE
+-----------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(200) | YES | | NULL | |
| surname | varchar(200) | YES | | NULL | |
| sex | varchar(1) | YES | | NULL | |
| idNatBirth | int(11) | YES | MUL | NULL | |
| idNatOrigin | int(11) | YES | MUL | NULL | |
| idNatCitizen | int(11) | YES | MUL | NULL | |
+-----------------------+--------------+------+-----+---------+----------------+
国家ids(出生,出生,公民身份)加入国家表:
NATIONS
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| code | bigint(20) | YES | | NULL | |
| description | varchar(100) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
我已经获得了三个按国家统计人数的表格。 对于三个国家的领域是同一个模型。这是为了诞生:
select people.idNatBirth, nations.*, count(*) FROM people
INNER JOIN nations
ON nations.id = people.idNatBirth
GROUP BY nations.description;
+------------------+-----+--------+-------------+----------+
| idNatBirth | id | code | description | count(*) |
+------------------+-----+--------+-------------+----------+
| 133 | 133 | 1 | AFGHANISTAN | 43 |
| 193 | 193 | 61 | IRAQ | 139 |
| 219 | 219 | 87 | PAKISTAN | 59 |
| 238 | 238 | 106 | SIRIA | 161 |
+------------------+-----+--------+-------------+----------+
但是我需要一个更复杂的结果,显示每一个数字的出生,起源,公民身份和性别分布。 这样的事情:
+------------------------+----------+------------+----------+------------+------------+-----------+------------+
| description | birth | birthMale | birthFem | origin | originMale | originFem | citizenship etc .....
+------------------------+----------+------------+----------+------------+------------+-----------+------------+
| AFGHANISTAN | 43 | 20 | 23 | 40 | 20 | 20 | ....
| IRAQ | 139 | 39 | 100 | 29 | 9 | 20 | ....
| PAKISTAN | 59 | 50 | 9 | 10 | 9 | 1 | ....
| SIRIA | 161 | 61 | 100 | 102 | 2 | 100 | ....
| ALBANIA | 0 | 0 | 0 | 12 | 10 | 2 | ....
| SOMALIA | 0 | 0 | 0 | ...
...
...
正如你所看到的那样,不是每个国家都存在于每个领域的人们... 有关如何实现的任何想法? 提前致谢。 这是人员表的示例数据:
+----+------------+------------+-------+------------------+----------------------+-----------------------+
| id | name | surname | sex | idNatBirth | idNatOrigin | idNatCitizen |
+----+------------+------------+-------+------------------+----------------------+-----------------------+
| 1 | name | surname | M | 133 | NULL | NULL |
| 2 | AUTOS1 | AUTOS2 | M | 133 | NULL | NULL |
| 3 | Johari | JAI | F | 193 | 238 | 193 |
| 4 | Bejide | RAI | F | 193 | 193 | 193 |
| 5 | Sema | DARSHA | M | 238 | 238 | 238 |
| 6 | Kefilwe | LATA | M | 219 | 219 | 219 |
| 7 | Kali | RAJAN | M | 238 | 238 | 238 |
| 8 | Tanginika | SAXENA | F | 193 | 193 | 193 |
| 9 | Marahaba | CHAUHAN | M | 238 | 238 | 238 |
| 10 | Malkia | PATEL | M | 238 | 238 | 238 |
| 11 | Makena | DHAWAN | F | 193 | 193 | 193 |
| 12 | Dziko | NARAYAN | M | 238 | 238 | 238 |
| 13 | Anaya | NEEL | M | 193 | 193 | 193 |
| 14 | Yahminah | SANDEEP | M | 238 | 238 | 238 |
| 15 | Iruwa | SEN | M | 219 | 219 | 219 |
| 16 | Abena | MATI | M | 133 | 133 | 133 |
| 17 | Obax | BHATT | M | 193 | 193 | 193 |
| 18 | Bikilu | NATH | F | 133 | 133 | 133 |
| 19 | Mumbi | SRINI | M | 219 | 219 | 219 |
| 20 | Wanjiko | MANJU | M | 193 | 238 | 193 |
+----+------------+------------+-------+------------------+----------------------+-----------------------+
答案 0 :(得分:0)
您可以尝试以下
SELECT description,
SUM(birth) birth,
SUM(birthmale) birthMale,
SUM(birthfem) birthFem,
SUM(origin) origin,
SUM(originmale) originMale,
SUM(originfem) originFem,
SUM(citizenship) citizenship,
SUM(citizenmale) citizenMale,
SUM(citizenfem) citizenFem
FROM (SELECT CASE
WHEN n.id IS NOT NULL THEN n.description
WHEN org.id IS NOT NULL THEN org.description
WHEN cit.id IS NOT NULL THEN cit.description
END description,
SUM(CASE WHEN n.id IS NOT NULL THEN 1 ELSE 0 END) AS birth,
SUM(CASE WHEN n.id IS NOT NULL AND people.sex = 'M' THEN 1 ELSE 0 END) birthmale,
SUM(CASE WHEN n.id IS NOT NULL AND people.sex = 'F' THEN 1 ELSE 0 END) birthFem,
SUM(CASE WHEN org.id IS NOT NULL THEN 1 ELSE 0 END) AS origin,
SUM(CASE WHEN org.id IS NOT NULL AND people.sex = 'M' THEN 1 ELSE 0 END) originmale,
SUM(CASE WHEN org.id IS NOT NULL AND people.sex = 'F' THEN 1 ELSE 0 END) originFem,
SUM(CASE WHEN cit.id IS NOT NULL THEN 1 ELSE 0 END) AS citizenship,
SUM(CASE WHEN cit.id IS NOT NULL AND people.sex = 'M' THEN 1 ELSE 0 END) citizenmale,
SUM(CASE WHEN cit.id IS NOT NULL AND people.sex = 'F' THEN 1 ELSE 0 END) citizenFem
FROM people
LEFT JOIN nations n
ON n.id = people.idnatbirth
LEFT JOIN nations org
ON org.id = people.idnatorigin
LEFT JOIN nations cit
ON cit.id = people.idnatcitizen
GROUP BY n.id,
org.id,
cit.id,
n.description,
org.description,
cit.description)t1
GROUP BY description;
希望这会帮助你。这真是一个简单的查询。格式化查询显示它像恶魔查询。