Reputation: 1154
I have MySQL table with structure:
| uid | itemid
| -------------------
| 1001 | 9999
| 1001 | 8888
| 2002 | 8888
| ... | ...
uid and itemid not unique columns. Bun uid+itemid is unique combination.
I need count rows by itemid. I use next query:
SELECT itemid, COUNT(*)
FROM unlikes
WHERE itemid IN ('9999', '8888') GROUP BY itemid;
And this works!
Next, i need to know which row have concrete uid. I use:
SELECT itemid, COUNT(*), IF (uid = '1001', TRUE, FALSE) toggled
FROM unlikes
WHERE itemid IN ('9999', '8888') GROUP BY itemid;'
Not work. If i have more than one row with itemid = 8888, than toggled = FALSE. I need toggled = TRUE if one in group have uid = 1001;
Upvotes: 0
Views: 251
Reputation: 32612
SELECT itemid, COUNT(*),
CASE WHEN uid = 1001 THEN TRUE ELSE FALSE end AS toggled
FROM unlikes
WHERE itemid IN ('9999', '8888') GROUP BY itemid;
Upvotes: 1