简体   繁体   中英

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)

try this

SELECT COUNTRY, LANGUAGE FROM table WHERE LANGUAGE IN (SELECT LANGUAGE FROM table GROUP BY language HAVING COUNT(language) > 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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