我想将与同一个人相关的所有四种类型的划分(婚姻、出生/死亡、代理婚姻、代理出生/死亡)显示在一行中。以下是我的数据库表结构
登记员表-
registrar id | name
1 | reg1
registrar_has_division 表-
id | registrar id | division id | is_acting
1 | 1 | 10 |0
2 | 1 | 11 |0
3 | 1 | 12 |1
4 | 1 | 13 |1
划分表-
division id | division name | division type
10 | Hulftsdrop | 1
11 | Modara | 2
12 | Slave Island | 1
13 | Fort | 2
这里划分类型1是婚姻划分,2是出生/死亡划分
以下是我写的查询 -
SELECT
r.id AS rid,
r.name,
IF(
divi.div_type = 1 && rd.is_acting = 0,
divi.name_english,
NULL
) AS marriage_div,
IF(
divi.div_type = 2 && rd.is_acting = 0,
divi.name_english,
NULL
) AS bd_div,
IF(
divi.div_type = 1 && rd.is_acting = 1,
divi.name_english,
NULL
) AS acting_marriage_div,
IF(
divi.div_type = 2 && rd.is_acting = 1,
divi.name_english,
NULL
) AS acting_bd_div
FROM
`registrar` AS `r`
INNER JOIN `registrar_has_division` AS `rd`
ON
`rd`.`registrar_id` = `r`.`id`
INNER JOIN `registrar_division` AS `divi`
ON
`rd`.`division_id` = `divi`.`id`
结果-
rid | name | marriage_div | b/d_div | acting_marriage_div | acting_bd_div
1 | reg1 | NULL | Modara | NULL | NULL
1 | reg1 | Hulftsdrop | NULL | NULL | NULL
1 | reg1 | NULL | NULL | Slave Island | NULL
1 | reg1 | NULL | NULL | NULL | Fort
我想要的结果如下
rid | name | marriage_div | b/d_div | acting_marriage_div | acting_bd_div
1 | reg1 | Hulftsdrop | Modara | Slave Island | Fort
我认为问题出在查询中的 if else 语句中。我尝试了很多,但我失败了。任何帮助将不胜感激。
翻翻过去那场雪