如何将这个SELECT转换为带有字符串的输出(在此示例下方)?
nose_1
|id|class|about_education|
| 1| 1| a01|
| 2| 0| a02|
coins_2
|id|class|about_education|
| 1| 1| a01|
| 2| 0| a02|
money_3
|id|class|about_education|
| 1| 1| a01|
| 2| 1| a10|
SELECT
COUNT(CASE WHEN about_education = 'a01' and class = 1 then 1 else 0 end) a01,
COUNT(CASE WHEN about_education = 'a02' and class = 1 then 1 else 0 end) a02,
COUNT(CASE WHEN about_education = 'a03' and class = 1 then 1 else 0 end) a03,
COUNT(CASE WHEN about_education = 'a04' and class = 1 then 1 else 0 end) a04,
COUNT(CASE WHEN about_education = 'a05' and class = 1 then 1 else 0 end) a05,
COUNT(CASE WHEN about_education = 'a06' and class = 1 then 1 else 0 end) a06,
COUNT(CASE WHEN about_education = 'a07' and class = 1 then 1 else 0 end) a07,
COUNT(CASE WHEN about_education = 'a08' and class = 1 then 1 else 0 end) a08,
COUNT(CASE WHEN about_education = 'a09' and class = 1 then 1 else 0 end) a09,
COUNT(CASE WHEN about_education = 'a10' and class = 1 then 1 else 0 end) a10
FROM
(
SELECT id,class,about_education from nose_1
UNION ALL
SELECT id,class,about_education FROM coins_2
UNION ALL
SELECT id,class,about_education FROM money_3
) t
输出:
$check_a01 = 3;
$check_a02 = 0;
$check_a03 = 0;
$check_a04 = 0;
$check_a05 = 0;
$check_a06 = 0;
$check_a07 = 0;
$check_a08 = 0;
$check_a09 = 0;
$check_a10 = 1;
Lorem ipsum dolor坐下,私服精英人士,sed do eiusmod tempor incididunt ut Labore et dolore magna aliqua。尽量不要抽烟,不要因抽烟而锻炼,而要从中抽签。
答案 0 :(得分:1)
您可以在THEN之后的所有1放在引号中,而在ELSE之后的所有0放在引号中。
then '1' else '0' end
答案 1 :(得分:0)
我不理解您的要求,希望下面的代码对您有所帮助
SELECT
CONCAT('$check_',
about_education,
' = ',
SUM(CASE
WHEN
SUBSTRING(t.about_education,
2,
LENGTH(t.about_education)) = a.id
AND class = 1
THEN
1
ELSE 0
END),
';') AS output
FROM
(SELECT
class, about_education
FROM
nose_1 UNION ALL SELECT
class, about_education
FROM
coins_2 UNION ALL SELECT
class, about_education
FROM
money_3) t
JOIN
(SELECT 1 AS Id UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10) a ON SUBSTRING(t.about_education,
2,
LENGTH(t.about_education)) = a.id
GROUP BY t.about_education;