【发布时间】:2018-11-19 08:44:31
【问题描述】:
我需要选择非官方 lang-s 是官方两倍以上的国家,+ 官方是 2+
MYSQL 查询:
SELECT c2.countrycode , sum(c2.isOfficial) as isFalse
FROM countrylanguage as c2
INNER JOIN (
select c.countrycode , sum(c.isOfficial)as isOffTrue
from countrylanguage as c
where c.isOfficial='T'
group by c.countrycode
having sum(c.isOfficial)>1)
) as cisT
ON cisT.countrycode = c2.countrycode
where c2.isOfficial='F'
group by c2.countrycode
having sum(c2.isOfficial)>cisT.isOffTrue*2
但我收到别名错误,无法定义问题的根本原因, 你能帮帮我吗?
稍后……
RC: extra ) 加入
NExt 错误:无法识别内部 sum() 别名,您能帮忙吗?
SELECT c2.countrycode , sum(c2.isOfficial) as isFalse
FROM countrylanguage as c2
INNER JOIN (
select c.countrycode , sum(c.isOfficial) isOffTrue
from countrylanguage as c
where c.isOfficial='T'
group by c.countrycode
having sum(c.isOfficial)>1
) as cisT
ON cisT.countrycode = c2.countrycode
where c2.isOfficial='F'
group by c2.countrycode
having sum(c2.isOfficial)>(cisT.isOffTrue*2);
错误 1054 (42S22):“有子句”中的未知列“cisT.isOffTrue”
加法:
表:
+-------------+---------------+------+-----+--------+
| Field | Type | Null | Key | Default |
+-------------+---------------+------+-----+---------+
| CountryCode | char(3) | NO | PRI | |
| Language | char(30) | NO | PRI | |
| IsOfficial | enum('T','F') | NO | | F |
+-------------+---------------+------+-----+---------+
我像下一个一样更改了查询并且它有效,购买我仍然没有得到之前失败的 RC
SELECT c2.countrycode, sum(c2.isOfficial) as isOffFalse, c1.isOffTrue
FROM (
select c0.countrycode, sum(c0.isOfficial)as isOffTrue
from countrylanguage c0
where c0.isOfficial='T'
group by c0.countrycode having sum(isOfficial)>1
) as c1, countrylanguage as c2
where c1.countrycode = c2.countrycode
and c2.isOfficial='F'
group by c2.countrycode
having sum(c2.isOfficial)>(c1.isOffTrue*2);
【问题讨论】:
-
此代码似乎不会产生该错误。您的代码还有其他问题,但不是那个问题。您应该在问题中包含样本数据、期望的结果以及对问题的清晰陈述。
-
@GordonLinoff 所说的错误之一是滥用 MySQL GROUP BY 功能。psce.com/en/blog/2012/05/15/…
-
一些错误:
sum()在字符常量上,滥用group by,在外部select中选择了错误的列,不平衡的括号。实际上,现在我仔细观察,最后一个正在产生您的特定错误。 -
更新问题
-
我发现了问题——有一个额外的)现在有下一个问题——在没有看到内部 sum() 别名中你能帮忙吗?
标签: mysql sql mysql-error-1054