MySQL从多个垂直表中选择并水平显示

时间:2015-12-07 11:14:05

标签: mysql select horizontallist

我有以下4个表格:

account           sub                   sub_props         sub_prop_vals
|id|number|       |id|account_id|       |id|sub_id|       |id|key|value|
|01|123456|       |02|01|               |03|02|           |03|notifMethod|SM|
                                        |04|02|           |04|notifStatus|true| 
|02|234567|       |03|02|               |05|03|           |05|notifStatus|true|
|03|345678|       |04|03|               |06|04|           |06|notifMethod|EM|
                                        |07|04|           |07|notifStatus|false|

应显示:

number;account_id;notifMethod;notifStatus
123456;01;SM;true
234567;02;null;true
345678;03;EM;false

我希望所有表格中的数据显示在每个account.number和sub.account_id的单个水平线上。我已尝试过这些行,但根据我的情况,它没有正确显示值。该查询将根据我的案例为每个“点击”创建一行。

select
ac.number,
ss.account_id,
case
   when spv.key = 'notifMethod' and spv.value = 'SM' then 'SM'
   when spv.key = 'notifMethod' and spv.value = 'EM' then 'EM'
end as notifMethod,
case
   when spv.key = 'notifStatus' and spv.value = 'Enabled' the 'true'
   when spv.key = 'notifStatus' and spv.value = 'Disabled' then 'false'
end as notifStatus
from account ac
inner join sub ss on ac.id = ss.account_id
inner join sub_props sp on ss.id = sp.sub_id
inner join sub_prop_vals spv on sp.id = spv.id
group by ac.number, sub.account_id;

0 个答案:

没有答案