Use of HAVING in MySQL

Posted by KBrian on Stack Overflow See other posts from Stack Overflow or by KBrian
Published on 2012-06-21T01:01:43Z Indexed on 2012/06/21 3:17 UTC
Read the original article Hit count: 285

Filed under:
|

I have a table from which I need to select all persons that have a first name that is not unique and that that set should be selected only if among the persons with a similar first name, all have a different last name.

Example:

FirstN    LastN
Bill      Clinton
Bill      Cosby
Bill      Maher
Elvis     Presley
Elvis     Presley
Largo     Winch

I want to obtain

FirstN    LastN
Bill      Clinton

or

FirstN    LastN
Bill      Clinton
Bill      Cosby
Bill      Maher

I tried this but it does not return what I want.

SELECT * FROM Ids
GROUP BY FirstN, LastN
HAVING (COUNT(FirstN)>1 AND COUNT(LastN)=1))

[Edited my post after Aleandre P. Lavasseur remark]

© Stack Overflow or respective owner

Related posts about mysql

Related posts about sql