如何在 WHERE 中使用 AS 和别名?
How can I use AS and the alias in WHERE?
嗨,我想检查 NumOfSpeakLanguages 是否有超过 10 种语言,我该怎么做:
这里的代码:
SELECT country.Name,country.Capital,COUNT(DISTINCT countrylanguage.Language) AS NumOfSpeakLanguages
FROM country,countrylanguage
WHERE country.Code=countrylanguage.CountryCode AND NumOfSpeakLanguages >10
GROUP BY country.Name
这里是错误代码:
SELECT country.Name,country.Capital,COUNT(DISTINCT countrylanguage.Language) AS NumOfSpeakLanguages
FROM country,countrylanguage
WHERE country.Code=countrylanguage.CountryCode AND NumOfSpeakLanguages >10
GROUP BY country.Name LIMIT 0, 25
#1054 - Unknown column'NumOfSpeakLanguages' in 'where clause'
谢谢!
对聚合函数条件使用 HAVING
子句:
SELECT country.Name,country.Capital,COUNT(DISTINCT countrylanguage.Language) AS NumOfSpeakLanguages
FROM country,
JOIN countrylanguage
ON country.Code = countrylanguage.CountryCode
GROUP BY country.Name
HAVING COUNT(DISTINCT countrylanguage.Language) > 10
LIMIT 0, 25
@Andrew Brēza 评论道:你也可以说 HAVING NumOfSpeakLanguages >10
。 (这是一个 MySQL 特殊。)
也在做显式 JOIN
!
或者,在派生的 table:
中进行聚合
select * from
(
SELECT country.Name,country.Capital,COUNT(DISTINCT countrylanguage.Language) AS NumOfSpeakLanguages
FROM country,
JOIN countrylanguage
ON country.Code = countrylanguage.CountryCode
GROUP BY country.Name
) dt
where NumOfSpeakLanguages > 10
LIMIT 0, 25
嗨,我想检查 NumOfSpeakLanguages 是否有超过 10 种语言,我该怎么做: 这里的代码:
SELECT country.Name,country.Capital,COUNT(DISTINCT countrylanguage.Language) AS NumOfSpeakLanguages
FROM country,countrylanguage
WHERE country.Code=countrylanguage.CountryCode AND NumOfSpeakLanguages >10
GROUP BY country.Name
这里是错误代码:
SELECT country.Name,country.Capital,COUNT(DISTINCT countrylanguage.Language) AS NumOfSpeakLanguages
FROM country,countrylanguage
WHERE country.Code=countrylanguage.CountryCode AND NumOfSpeakLanguages >10
GROUP BY country.Name LIMIT 0, 25
#1054 - Unknown column'NumOfSpeakLanguages' in 'where clause'
谢谢!
对聚合函数条件使用 HAVING
子句:
SELECT country.Name,country.Capital,COUNT(DISTINCT countrylanguage.Language) AS NumOfSpeakLanguages
FROM country,
JOIN countrylanguage
ON country.Code = countrylanguage.CountryCode
GROUP BY country.Name
HAVING COUNT(DISTINCT countrylanguage.Language) > 10
LIMIT 0, 25
@Andrew Brēza 评论道:你也可以说 HAVING NumOfSpeakLanguages >10
。 (这是一个 MySQL 特殊。)
也在做显式 JOIN
!
或者,在派生的 table:
中进行聚合select * from
(
SELECT country.Name,country.Capital,COUNT(DISTINCT countrylanguage.Language) AS NumOfSpeakLanguages
FROM country,
JOIN countrylanguage
ON country.Code = countrylanguage.CountryCode
GROUP BY country.Name
) dt
where NumOfSpeakLanguages > 10
LIMIT 0, 25