Mysql: 创建一个具有多个自连接且结果不重复的视图
Mysql: Create a view with multiple self joins without duplicates in result
澄清一下,我无法更改 table 的结构,所以请省略 "you should change your tables to this and that" 答案,谢谢。
所以我有一个 table entities_attributes_values 其中一个实体有很多属性和该属性的值,基本上可以想象 3 个字段:
- entity_id
- entity_attributes_id
- 价值
因为每个实体属性及其值都在行上,所以获取更多值并不容易,我在考虑多个自连接,并且因为这个查询很常见,所以我创建了一个视图,它是用这个查询构建的:
SELECT `L1`.`entity_id`,
`L1`.`value` as 'company_id',
`L2`.`value` as 'entity_name',
`P`.`value` as 'person_name',
`L4`.`value` as 'establishment_id',
`L5`.`value` as 'department_id'
FROM `entities_attributes_values` `L1`
LEFT JOIN `entities_attributes_values` `L2` ON `L1`.`entity_id` = `L2`.`entity_id` AND `L2`.`entity_attributes_id` = 1
LEFT JOIN `entities_attributes_values` `L3` ON `L1`.`entity_id` = `L3`.`entity_id` AND `L3`.`entity_attributes_id` = 3
LEFT JOIN `persons_attributes_values` `P` ON `L3`.`value` = `P`.`core_persons_id` AND `P`.`core_persons_attributes_id` = 4
LEFT JOIN `entities_attributes_values` `L4` ON `L1`.`entity_id` = `L4`.`entity_id` AND `L4`.`entity_attributes_id` = 12
LEFT JOIN `entities_attributes_values` `L5` ON `L1`.`entity_id` = `L5`.`entity_id` AND `L5`.`entity_attributes_id` = 13
WHERE `L1`.`entity_attributes_id` = 2
所以这行得通,但我有一个问题,我得到 "duplicate" 值,它不是真正重复的,但 关键是,在我看来,我希望每个实体都只有一行它的属性值 但我得到的是:
所以你可以看到前三个结果对我来说不好,我只需要第四个,我有关于一个实体的所有数据。
提前感谢您的帮助!
尝试使用条件聚合:
select eav.entity_id,
max(case when entity_attributes_id = 2 then eav.value end) as company_id,
max(case when entity_attributes_id = 1 then eav.value end) as entity_name,
max(case when entity_attributes_id = 3 then eav.value end) as company_name,
. . .
from entities_attributes_values eav
group by eav.entity_id;
这将使向视图添加新属性变得容易。另外,不要使用单引号来分隔列名。单引号只能用于日期和时间常量。
澄清一下,我无法更改 table 的结构,所以请省略 "you should change your tables to this and that" 答案,谢谢。
所以我有一个 table entities_attributes_values 其中一个实体有很多属性和该属性的值,基本上可以想象 3 个字段:
- entity_id
- entity_attributes_id
- 价值
因为每个实体属性及其值都在行上,所以获取更多值并不容易,我在考虑多个自连接,并且因为这个查询很常见,所以我创建了一个视图,它是用这个查询构建的:
SELECT `L1`.`entity_id`,
`L1`.`value` as 'company_id',
`L2`.`value` as 'entity_name',
`P`.`value` as 'person_name',
`L4`.`value` as 'establishment_id',
`L5`.`value` as 'department_id'
FROM `entities_attributes_values` `L1`
LEFT JOIN `entities_attributes_values` `L2` ON `L1`.`entity_id` = `L2`.`entity_id` AND `L2`.`entity_attributes_id` = 1
LEFT JOIN `entities_attributes_values` `L3` ON `L1`.`entity_id` = `L3`.`entity_id` AND `L3`.`entity_attributes_id` = 3
LEFT JOIN `persons_attributes_values` `P` ON `L3`.`value` = `P`.`core_persons_id` AND `P`.`core_persons_attributes_id` = 4
LEFT JOIN `entities_attributes_values` `L4` ON `L1`.`entity_id` = `L4`.`entity_id` AND `L4`.`entity_attributes_id` = 12
LEFT JOIN `entities_attributes_values` `L5` ON `L1`.`entity_id` = `L5`.`entity_id` AND `L5`.`entity_attributes_id` = 13
WHERE `L1`.`entity_attributes_id` = 2
所以这行得通,但我有一个问题,我得到 "duplicate" 值,它不是真正重复的,但 关键是,在我看来,我希望每个实体都只有一行它的属性值 但我得到的是:
所以你可以看到前三个结果对我来说不好,我只需要第四个,我有关于一个实体的所有数据。
提前感谢您的帮助!
尝试使用条件聚合:
select eav.entity_id,
max(case when entity_attributes_id = 2 then eav.value end) as company_id,
max(case when entity_attributes_id = 1 then eav.value end) as entity_name,
max(case when entity_attributes_id = 3 then eav.value end) as company_name,
. . .
from entities_attributes_values eav
group by eav.entity_id;
这将使向视图添加新属性变得容易。另外,不要使用单引号来分隔列名。单引号只能用于日期和时间常量。