[英]Create columns based on the same column name with different value
I have a table: 我有一张桌子:
As above image shows there are many other meta_appl_id's. 如上图所示,还有许多其他meta_appl_id。
Now I want to run an SQL query so that will create columns based on the value of "meta_value". 现在,我想运行一个SQL查询,以便根据“ meta_value”的值创建列。 I tried: 我试过了:
SELECT * ,
if (`meta_value` = 'physically challenged' , 'yes', '') as PH,
if (`meta_value` = 'kannada medium' , 'yes', '') as Kannada,
if (`meta_value` = 'rural' , 'yes', '') as rural,
if (`meta_value` = 'woman' , 'yes', '') as woman
FROM `applicant_meta` WHERE `meta_value` = 'physically challenged' or `meta_value` = 'woman' group by meta_appl_id;
but could not get the needed output, only one column value as shown: 但无法获得所需的输出,只有一个列值,如下所示:
Actually in row meta_appl_id 59 it have to show Ph - yes, rural - yes and woman yes but its only showing in woman (because it is last if statement in query). 实际上,在meta_appl_id 59行中,它必须显示Ph-是,农村-是,并且是yes,但是仅在女人中显示(因为如果查询中的语句为最后一个,则它)。
SELECT meta_appl_id, if(PH= 0, 'no', 'yes'), if(WOMAN= 0, 'no', 'yes')
FROM (
SELECT meta_appl_id
, (select count(1) FROM applicant_meta as innertab where UPPER(innertab.meta_appl_id) = UPPER(aa.meta_appl_id) and UPPER(meta_value)=UPPER('physically challenged')) as PH
, (select count(1) FROM applicant_meta as innertab where innertab.meta_appl_id = aa.meta_appl_id and UPPER(meta_value)='WOMAN') as WOMAN
FROM applicant_meta as aa
GROUP BY meta_appl_id
) as bb;
Please add other possible columns according to need. 请根据需要添加其他可能的列。 I have shown 2 columns here. 我在这里显示了2列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.