Let\'s say we have a Category - Items one-to-many relation. I would like to do this
SELECT c.*,
(SELECT COUNT(*) FROM items i W
Depending on your circumstances and ability to create a view. I would just create a view out of your query:
CREATE VIEW CategoryItemsView AS
SELECT c.*,
(SELECT COUNT(*) FROM items i WHERE i.catId=c.id)
AS itemCount
FROM category c
afterwards you can query how ever you like...
SELECT * FROM CategoryItemsView WHERE ItemCount = 5
Additionally, you could use a GROUP BY to achieve a similar result but that depends on your columns and the schema of your tables.
So, something like this:
SELECT c.COLUMN1, c.COLUMN2, COUNT(*) AS ItemCount
FROM category c inner join items i on i.catID = c.Id
GROUP BY c.COLUMN1, c.COLUMN2
HAVING COUNT(*) = 2