简体   繁体   English

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

[英]MySQL select where duplicates in field, not returning all records

I am trying to select records where the record shares a field value with another record, but the query I am trying only returns one occurrence. 我正在尝试选择记录与另一个记录共享一个字段值的记录,但是我尝试的查询仅返回一个实例。 For example, in this table: 例如,在此表中:

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

... I would like to return: ...我想返回:

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

Using: 使用方法:

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

I only get: 我只会得到:

COUNTRY         LANGUAGE
Spain           Spanish
United Kingdom  English

Where am I going wrong here? 我在哪里错了?

Found a solution: 找到了解决方案:

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

You need to break this up into two steps. 您需要将其分为两个步骤。 Get the list of languages in a sub-select, then get the rows: 获取子选择中的语言列表,然后获取行:

SELECT * FROM country_language where language in (SELECT language FROM country_language GROUP BY language HAVING COUNT(*) > 1) SELECT * FROM country_language其中的语言(选择语言FROM country_language GROUP BY语言的语言拥有计数(*)> 1)

try this 尝试这个

SELECT COUNTRY, LANGUAGE FROM table WHERE LANGUAGE IN (SELECT LANGUAGE FROM table GROUP BY language HAVING COUNT(language) > 1); SELECT COUNTRY,从table语言WHERE LANGUING IN(从table GROUP BY中选择语言, language HAVING COUNT(语言)> 1);

You need a sub query that returns the set of languages that have multiple countries speaking them. 您需要一个子查询,该查询返回一组具有多种国家/地区语言的语言。 Then select all of the rows whose language is in that set: 然后选择该语言集中的所有行:

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

I'm not that experienced with having but this should do the trick! 我没有那么有经验,但这应该可以解决问题!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM