MySQL选择字段中重复的位置,而不是返回所有记录

时间:2015-04-02 23:34:48

标签: mysql sql

我正在尝试选择记录与另一条记录共享字段值的记录,但我尝试的查询只返回一次。例如,在此表中:

COUNTRY         LANGUAGE
Mexico          Spanish
Portugal        Portuguese
Russia          Russian
Spain           Spanish
Thailand        Thai
United Kingdom  English
United States   English

...我想回来:

COUNTRY         LANGUAGE
Mexico          Spanish
Spain           Spanish
United Kingdom  English
United States   English

使用:

SELECT * FROM `table` GROUP BY `language` HAVING COUNT(language) > 1

我只得到:

COUNTRY         LANGUAGE
Spain           Spanish
United Kingdom  English

我在哪里错了?

5 个答案:

答案 0 :(得分:2)

找到解决方案:

SELECT * FROM `TABLE` WHERE `language` IN (
    SELECT * GROUP BY `language` HAVING COUNT(primary_language) > 1
)

答案 1 :(得分:1)

您需要将其分解为两个步骤。获取子选择中的语言列表,然后获取行:

SELECT * FROM country_language where language in(SELECT language FROM country_language GROUP BY language HAVING COUNT(*)> 1)

答案 2 :(得分:1)

试试这个

  

SELECT COUNTRY,LANGUAGE FROM table WHERE LANGUAGE IN(SELECT table SELECT BY language GROUP BY {{1}} COUNT(语言)> 1);

答案 3 :(得分:1)

您需要一个子查询,它返回多个国家/地区的语言集。然后选择语言在该集合中的所有行:

SELECT *
FROM `table`
WHERE language IN
(
  SELECT language FROM `table` GROUP BY `language` HAVING COUNT(*) > 1
) 

答案 4 :(得分:1)

我不是那么有经验,但这应该可以做到!

SELECT Country
from (SELECT Country, count(Lang) as Langs
FROM Land
Group by Lang) as table2
WHERE table2.Langs!=1