lundi 30 novembre 2015

Select from table based on count

I have table like this:

id | name | type
-----------------
 0 | firs |    2
 1 | secs |    3
 2 | this |    9
 1 | thus |    3

I know id (it is not unique id) and type and I want to select records only if there is specified number of records with that id and type.
For one record I tried for example this:

select * from myTable
where
(select count(*) from myTable where myTable.id = 0 and myTable.type = 2) = 1;

This returns me all rows, not just the one row I want. Can anyone please tell me, what is the right way how to get the right result?

Aucun commentaire:

Enregistrer un commentaire