根据具有不同值的相同列名创建列
Create columns based on the same column name with different value
我有一个table:
如上图所示,还有许多其他 meta_appl_id。
现在我想 运行 一个 SQL 查询,以便根据 "meta_value" 的值创建列。我试过了:
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;
但无法获得所需的输出,只有一列值,如图所示:
实际上在第 meta_appl_id 59 行中,它必须显示 Ph - 是,农村 - 是和女人是,但它只显示在女人中(因为它是查询中的最后一个 if 语句)。
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;
请根据需要添加其他可能的列。我在这里显示了 2 列。
我有一个table:
如上图所示,还有许多其他 meta_appl_id。
现在我想 运行 一个 SQL 查询,以便根据 "meta_value" 的值创建列。我试过了:
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;
但无法获得所需的输出,只有一列值,如图所示:
实际上在第 meta_appl_id 59 行中,它必须显示 Ph - 是,农村 - 是和女人是,但它只显示在女人中(因为它是查询中的最后一个 if 语句)。
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;
请根据需要添加其他可能的列。我在这里显示了 2 列。