【问题标题】:Need solution for the complex query matrix output in mariadb需要 mariadb 中复杂查询矩阵输出的解决方案
【发布时间】:2018-12-02 02:14:51
【问题描述】:
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);

我想要最终输出如下
final output

人 朋友 儿子 女儿 父亲 母亲 兄弟 妹妹 詹姆斯 - Y - - - - - 彼得 - Y - Y - Y - 约瑟夫 - - - 是 - - - 杰尼 - - 是 - - - -

由于底层数据库是 Maria DB,我不能使用数据透视函数。 请注意,人可以增加或减少/ 还有,明天的角色可以增加,比如爷爷、妻子等。

不确定如何在存储过程或查询中解决这个问题。

我们可以使用 XML DATA TYPE 来处理动态查询吗?

【问题讨论】:

  • 哪个版本的 MariaDB?在 10.2.1 或更高版本之前?
  • 10.2.1是db版本
  • 查看其他标记为数据透视表的问题。
  • mariaDB 无法使用数据透视表

标签: mysql stored-procedures mariadb pivot-table cross-join


【解决方案1】:

怎么样:

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         -       -       -        -

【讨论】:

  • 我不知道手头的角色列表即问题,否则解决方案是完美的
  • 啊...那么您需要动态列数。我不知道你是否可以在 MariaDB 中做到这一点。
【解决方案2】:

我认为目前无法在 MariaDB 中检索动态列数。

据我所知,此功能仅存在于:

  • Oracle(作为“模型”)。
  • PostgreSQL(作为“交叉表”)。
  • SQL Server(作为“枢轴”)。

【讨论】:

    【解决方案3】:

    我通过使用临时表、更改每个字段的临时表并使用正确的参数进行更新来解决它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      相关资源
      最近更新 更多