vendredi 20 mars 2015

How to select columns that have repeating values from another table in SQLite

I am following Zed Shaw's learnSQLthehardway and I wanted to figure out how to select the names from a table person, who owned multiple pets (pets information in table pet.)



person_pet Table:

person_id pet_id
0 0
0 1
1 1
1 2
2 3

person Table:

id name
0 Zed
1 Orange
2 Limen


pet Table:

id name
0 Jag
1 Black
2 Fluffy
3 Mister


I have been trying to use the COUNT function but I can't seem to get the right results. I must return Zed and Orange based off this data.



SELECT name FROM person, person_pet WHERE id =
(SELECT person_id FROM person_pet GROUP BY person_id HAVING COUNT(person_id) > 1);


This is only returning Zed and not my name. How? What could solve the problem then?


Any help would be great, thank you!


Aucun commentaire:

Enregistrer un commentaire