我是mysql
的新手,我很难准备要查询的查询,该查询将返回所有剩余项目(其余项目除外)
例如:
TableName:City
字段是:id,name,population
我想获得所有不相同的城市名称 我正在尝试类似的事情,但这给了我一个错误
SELECT S.CITY FROM STATION AS S LEFT JOIN STATION ON (SELECT DISTINCT(P.CITY) FROM STATION AS P)
有人可以指导我吗
答案 0 :(得分:3)
尝试这样的事情:
SELECT city, COUNT(city) AS count
FROM station
GROUP BY city
HAVING COUNT(city) > 1
答案 1 :(得分:1)
distinct不是函数而是子句
如果要使用distinct,则可以尝试使用不匹配的子查询left jont
SELECT S.CITY
FROM STATION AS S
LEFT JOIN STATION ON (
SELECT DISTINCT CITY
FROM STATION
) T S.CITY = T.CITY
where t.city is null
但是aso按城市分组,并且jave计数(*)= 1
SELECT CITY
FROM STATION
GROUP BY city
having count(*) = 1