mariadb中复杂查询矩阵输出的解决方案

dl5txlt9  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(412)
create table role (
  role varchar(20),
  id int
);

insert into role (role, id) values ('Friend', 1);
insert into role (role, id) values ('Son', 2);
insert into role (role, id) values ('Daughter', 3);
insert into role (role, id) values ('Father', 4);
insert into role (role, id) values ('Mother', 5);
insert into role (role, id) values ('Brother', 6);
insert into role (role, id) values ('Sister', 7);

create table person (
  persons varchar(20),
  personid int
);

insert into person (persons, personid) values ('James', 1);
insert into person (persons, personid) values ('Peter', 2);
insert into person (persons, personid) values ('Joseph', 3);
insert into person (persons, personid) values ('Jeni', 4);

create table role_person (
  roleid int,
  personid int
);

insert into role_person (roleid, personid) values (2, 1);
insert into role_person (roleid, personid) values (2, 2);
insert into role_person (roleid, personid) values (4, 2);
insert into role_person (roleid, personid) values (6, 2);
insert into role_person (roleid, personid) values (6, 2);
insert into role_person (roleid, personid) values (3, 4);
insert into role_person (roleid, personid) values (4, 3);

我希望最终输出如下
最终输出

persons  friend  son  daughter  father  mother  brother  sister
    James    -       Y    -         -       -       -        -
    Peter    -       Y    -         Y       -       Y        -
    Joseph   -       -    -         Y       -       -        -
    Jeni     -       -    Y         -       -       -        -

由于底层数据库是mariadb,我不能使用pivot函数。请注意,人们可以增加或减少/也可以增加角色,如祖父、妻子等。
不知道如何在存储过程或查询中处理这个问题。
我们可以使用xml数据类型来处理动态查询。

slmsl1lt

slmsl1lt1#

我认为到目前为止,在mariadb中检索动态数量的列是不可能的。
据我所知,这种功能只存在于:
甲骨文(作为“模型”)。
postgresql(作为“交叉表”)。
sql server(作为“pivot”)。

t30tvxxf

t30tvxxf2#

我通过使用临时表、更改每个字段的临时值并使用正确的参数更新来解决这个问题。

34gzjxbg

34gzjxbg3#

关于:

select 
    p.persons,
    case when 1 in (select 1 from role_person rp where rp.personid = p.personid and rp.roleid = 1) then 'Y' else '-' end as friend,
    case when 1 in (select 1 from role_person rp where rp.personid = p.personid and rp.roleid = 2) then 'Y' else '-' end as son,
    case when 1 in (select 1 from role_person rp where rp.personid = p.personid and rp.roleid = 3) then 'Y' else '-' end as daughter,
    case when 1 in (select 1 from role_person rp where rp.personid = p.personid and rp.roleid = 4) then 'Y' else '-' end as father,
    case when 1 in (select 1 from role_person rp where rp.personid = p.personid and rp.roleid = 5) then 'Y' else '-' end as mother,
    case when 1 in (select 1 from role_person rp where rp.personid = p.personid and rp.roleid = 6) then 'Y' else '-' end as brother,
    case when 1 in (select 1 from role_person rp where rp.personid = p.personid and rp.roleid = 7) then 'Y' else '-' end as sister
  from person p;

结果:

persons  friend  son  daughter  father  mother  brother  sister
James    -       Y    -         -       -       -        -
Peter    -       Y    -         Y       -       Y        -
Joseph   -       -    -         Y       -       -        -
Jeni     -       -    Y         -       -       -        -

相关问题