So the Table structure is this
CD id | Artist id
I want to return artists that appear more than once in this table, as in with more than one CD.
I’m using Mysql so what is the best SQL query to do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First you group your records by the artist_id, then you can use the
HAVINGclause, which allows you to filter data based on an aggregate, such asCOUNT, orSUMShort explanation:
This query will select the artist, as well as a count of the number of times they are in the table (Number of CDs in this instance)
This
COUNTis aggregated on theGROUP BYclause, so it will count each grouping ofArtist_IdThe
HAVINGclause then says that we only want to return records that have aCOUNT(1) > 1which will indicate number of CDs for this table.