我有结构的 MySQL 表:
| uid | itemid
| -------------------
| 1001 | 9999
| 1001 | 8888
| 2002 | 8888
| ... | ...
uid和itemid不是唯一的列。Bun uid+itemid 是独特的组合。
我需要按 itemid 计算行数。我使用下一个查询:
SELECT itemid, COUNT(*)
FROM unlikes
WHERE itemid IN ('9999', '8888') GROUP BY itemid;
这行得通!
接下来,我需要知道哪一行有具体的 uid。我用:
SELECT itemid, COUNT(*), IF (uid = '1001', TRUE, FALSE) toggled
FROM unlikes
WHERE itemid IN ('9999', '8888') GROUP BY itemid;'
不行。如果我有不止一行 itemid = 8888,则切换 = FALSE。如果组中有一个 uid = 1001,我需要 toggled = TRUE;