MySQL动态交叉表查询:选择子记录作为附加列

时间:2015-12-10 16:19:16

标签: mysql sql denormalization

我在MySQL数据库中有三个表:

users(user_settings,setting_types)

setting_types(id,name)

user_settings(value,user_id,setting_type_id)

我想对这些进行非规范化处理,以便在查询数据库时得到如下结果:

User.id    User.username    setting_name_1    setting_name_2    etc...
1          Admin            true              false
2          User             false             false

手头的问题是设置类型系统是可扩展的:我不一定知道用户将提前拥有哪些设置,因此我无法将它们直接硬编码到查询中。

是否可以通过user_setting为查询返回包含每个子setting_type的附加列的用户记录的方式进行查询?或者这是否超出了MySQL?

1 个答案:

答案 0 :(得分:1)

<强>计划

  
      
  • 编写数据透视源查询以获取数据透视所需的所有信息
  •   
  • 生成动态sql以重复加入和转动源
  •   

<强>设置

create table users
(
  id integer primary key not null,
  username varchar(23) not null
  -- some user data..
);

create table setting_types
(
  id integer primary key not null,
  name varchar(23) not null
);

create table user_settings
(
  id integer primary key not null,
  user_id integer not null,
  setting_type_id integer not null,
  value varchar(13) not null,
  foreign key ( user_id ) references users( id ),
  foreign key ( setting_type_id ) references setting_types ( id )
);

insert into users
( id, username )
values
( 1, 'Admin' ),
( 2, 'heresjonny' )
;

insert into setting_types
( id, name )
values
( 1, 'setting_type_1' ),
( 2, 'setting_type_2' ),
( 3, 'setting_type_3' ),
( 4, 'setting_type_4' ),
( 5, 'setting_type_5' ),
( 6, 'setting_type_6' ),
( 7, 'setting_type_7' ),
( 8, 'setting_type_8' )
;

insert into user_settings
( id, user_id, setting_type_id, value )
values
( 1, 1, 1, 'true' ),
( 2, 1, 2, 'false' ),
( 3, 1, 3, 'false' ),
( 4, 1, 4, 'false' ),
( 5, 2, 3, 'true' ),
( 6, 2, 4, 'true' ),
( 7, 2, 5, 'false' ),
( 8, 2, 6, 'true' ),
( 9, 2, 7, 'true' ),
( 10, 2, 8, 'true' )
;

<强>枢轴

set @pivot_source = '(
select st.id as setting_id, st.name, users.id as user_id, users.username, coalesce(us.value, ''false'') as value
from setting_types st
cross join
(
  select id, username
  from users
) users
left join user_settings us
on  users.id = us.user_id
and st.id    = us.setting_type_id
)';

set @pivot_sql := replace('
select user_id, username,
#setting_aliases#
from
(
select #first_user_dets#,
#settings_fields#
from 
#pivot_source# #first_alias#
inner join
#all_joins#
) q
order by user_id
;', '#pivot_source#', @pivot_source);

set @pivot_block := replace('
#pivot_source# #alias# 
on  #last_alias#.user_id = #alias#.user_id
and #last_alias#.setting_id < #alias#.setting_id 
inner join #all_joins#', '#pivot_source#', @pivot_source)
;

select count(*) into @ignore
from
(
select 
@pivot_sql := replace(@pivot_sql, '#all_joins#', replace(replace(@pivot_block, '#alias#', concat('sett', right_id)), '#last_alias#', concat('sett', left_id)))
from
(
select `left`.id as left_id, min(`right`.id) as right_id
from setting_types `left`
inner join setting_types `right`
on `left`.id < `right`.id
group by 1
) t
order by left_id
) `ignore`
;

select concat('sett', id) into @first_alias
from setting_types
order by id
limit 1
;

select concat(@first_alias, '.user_id,',@first_alias,'.username') into @first_user_dets;

select group_concat(concat('sett', id, '.value ', name) SEPARATOR ',') into @settings_fields
from setting_types
;

select group_concat(name SEPARATOR ',') into @setting_aliases
from setting_types
;

select count(*) into @ignore
from
(
select
@pivot_sql := replace(@pivot_sql, '#first_user_dets#', @first_user_dets),
@pivot_sql := replace(@pivot_sql, '#settings_fields#', @settings_fields),
@pivot_sql := replace(@pivot_sql, '#setting_aliases#', @setting_aliases),
@pivot_sql := replace(@pivot_sql, '#first_alias#', @first_alias),
@pivot_sql := replace(@pivot_sql, 'inner join #all_joins#', '')
) `ignore`
;

select @pivot_sql;

prepare pivot_sql from @pivot_sql;
EXECUTE pivot_sql;
deallocate prepare pivot_sql;

<强>输出

+---------+------------+----------------+----------------+----------------+----------------+----------------+----------------+----------------+----------------+
| user_id |  username  | setting_type_1 | setting_type_2 | setting_type_3 | setting_type_4 | setting_type_5 | setting_type_6 | setting_type_7 | setting_type_8 |
+---------+------------+----------------+----------------+----------------+----------------+----------------+----------------+----------------+----------------+
|       1 | Admin      | true           | false          | false          | false          | false          | false          | false          | false          |
|       2 | heresjonny | false          | false          | true           | true           | false          | true           | true           | true           |
+---------+------------+----------------+----------------+----------------+----------------+----------------+----------------+----------------+----------------+

<强> sqlfiddle

注意

在应用程序代码中进行此类转换更为常见。如果你这样做的原因是为了提高性能,那么应该根据php中的类似转换进行基准测试来测试实际上是否明显更好..

可能会发现我在pivoting with dynamic columns上的早期答案对于开发用于基准测试性能的PHP代码非常有用