SQL 使用多个子查询的字段列表中的错误未知列
SQL error Unknown column in field list using multiple subquery
我正在尝试打印基于列的文本。我的想法是联合每个职业的所有计数并用一个案例来展示它。但我不确定这段代码有什么问题。由于 sCount 不在字段列表中而出现错误。感谢任何帮助。
SELECT CASE
WHEN Occupation = "doctor" THEN
CONCAT("There are a total of ", dCount, " " , Occupation, "s.")
WHEN Occupation = "singer" THEN
CONCAT("There are a total of ", sCount, " " , Occupation, "s.")
END
FROM (
SELECT * FROM (
SELECT COUNT(Occupation) AS dCount, Lower(Occupation) AS Occupation FROM OCCUPATIONS WHERE Occupation = 'Doctor'
UNION
SELECT COUNT(Occupation) AS sCount, Lower(Occupation) AS Occupation FROM OCCUPATIONS WHERE Occupation = 'Singer'
) AS s
) AS m;
// this didnt work either
SELECT CASE
WHEN Occupation = "doctor" THEN
CONCAT("There are a total of ", dCount, " " , Occupation, "s.")
WHEN Occupation = "singer" THEN
CONCAT("There are a total of ", sCount, " " , Occupation, "s.")
END
FROM (
SELECT COUNT(Occupation) AS dCount, Lower(Occupation) AS Occupation FROM OCCUPATIONS WHERE Occupation = 'Doctor'
UNION
SELECT COUNT(Occupation) AS sCount, Lower(Occupation) AS Occupation FROM OCCUPATIONS WHERE Occupation = 'Singer'
) AS m;
你在尝试这样的事情吗:
CREATE TABLE OCCUPATIONS (
Occupation varchar(100) );
INSERT INTO OCCUPATIONS VALUES ('doctor'),('doctor'),
('singer'),('singer'),
('doctor'),('doctor'),
('singer'),('singer'),
('doctor'),('singer'),
('singer'),('singer');
SELECT CASE
WHEN Occupation = "doctor" THEN
CONCAT("There are a total of ", tbl.dCount, " " , Occupation, "s.")
WHEN Occupation = "singer" THEN
CONCAT("There are a total of ", tbl.sCount, " " , Occupation, "s.")
END as total_count
FROM (
SELECT COUNT(Occupation) AS dCount,null as sCount, Lower(Occupation) AS Occupation
FROM OCCUPATIONS
WHERE Occupation = 'doctor'
group by Occupation
union
SELECT null as tst, COUNT(Occupation) AS sCount, Lower(Occupation) AS Occupation
FROM OCCUPATIONS
WHERE Occupation = 'singer'
group by Occupation
)
as tbl ;
基于我的演示的结果是:
total_count
There are a total of 5 doctors.
There are a total of 7 singers.
我正在尝试打印基于列的文本。我的想法是联合每个职业的所有计数并用一个案例来展示它。但我不确定这段代码有什么问题。由于 sCount 不在字段列表中而出现错误。感谢任何帮助。
SELECT CASE
WHEN Occupation = "doctor" THEN
CONCAT("There are a total of ", dCount, " " , Occupation, "s.")
WHEN Occupation = "singer" THEN
CONCAT("There are a total of ", sCount, " " , Occupation, "s.")
END
FROM (
SELECT * FROM (
SELECT COUNT(Occupation) AS dCount, Lower(Occupation) AS Occupation FROM OCCUPATIONS WHERE Occupation = 'Doctor'
UNION
SELECT COUNT(Occupation) AS sCount, Lower(Occupation) AS Occupation FROM OCCUPATIONS WHERE Occupation = 'Singer'
) AS s
) AS m;
// this didnt work either
SELECT CASE
WHEN Occupation = "doctor" THEN
CONCAT("There are a total of ", dCount, " " , Occupation, "s.")
WHEN Occupation = "singer" THEN
CONCAT("There are a total of ", sCount, " " , Occupation, "s.")
END
FROM (
SELECT COUNT(Occupation) AS dCount, Lower(Occupation) AS Occupation FROM OCCUPATIONS WHERE Occupation = 'Doctor'
UNION
SELECT COUNT(Occupation) AS sCount, Lower(Occupation) AS Occupation FROM OCCUPATIONS WHERE Occupation = 'Singer'
) AS m;
你在尝试这样的事情吗:
CREATE TABLE OCCUPATIONS (
Occupation varchar(100) );
INSERT INTO OCCUPATIONS VALUES ('doctor'),('doctor'),
('singer'),('singer'),
('doctor'),('doctor'),
('singer'),('singer'),
('doctor'),('singer'),
('singer'),('singer');
SELECT CASE
WHEN Occupation = "doctor" THEN
CONCAT("There are a total of ", tbl.dCount, " " , Occupation, "s.")
WHEN Occupation = "singer" THEN
CONCAT("There are a total of ", tbl.sCount, " " , Occupation, "s.")
END as total_count
FROM (
SELECT COUNT(Occupation) AS dCount,null as sCount, Lower(Occupation) AS Occupation
FROM OCCUPATIONS
WHERE Occupation = 'doctor'
group by Occupation
union
SELECT null as tst, COUNT(Occupation) AS sCount, Lower(Occupation) AS Occupation
FROM OCCUPATIONS
WHERE Occupation = 'singer'
group by Occupation
)
as tbl ;
基于我的演示的结果是:
total_count
There are a total of 5 doctors.
There are a total of 7 singers.