0

我有结构的 MySQL 表:

| uid     |   itemid
| -------------------
| 1001    |   9999
| 1001    |   8888
| 2002    |   8888
| ...     |   ...

uiditemid不是唯一的列。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;

4

1 回答 1

1
SELECT itemid, COUNT(*), 
CASE WHEN uid = 1001 THEN TRUE ELSE FALSE end AS toggled 
FROM unlikes 
WHERE itemid IN ('9999', '8888') GROUP BY itemid;
于 2012-09-08T14:12:09.320 回答