mysql COUNT DISTINCT多列与WHERE

时间:2013-06-18 16:30:24

标签: mysql mysql-error-1241

我正在尝试从单个表生成数据,该表创建一个报告,显示上一个时间段(@dt)中位置a,b,c的次数。

我可以使单个SELECT查询工作,但无法弄清楚如何将它们组合在一起并生成完整的报告。错误是1241:操作数应该包含1列,我花了一个小时处理以前的答案,但我完全陷入困境。

SELECT (
SELECT DISTINCT pid, SUM(location like '%a%') FROM db.t WHERE (date > @dt) GROUP BY pid)
 AS 'a', (
SELECT DISTINCT pid, SUM(location like '%b%') FROM db.t WHERE (date > @dt) GROUP BY pid)
 AS 'b', (
SELECT DISTINCT pid, SUM(location like '%c%') FROM db.t WHERE (date > @dt) GROUP BY pid)
 AS 'c';

1 个答案:

答案 0 :(得分:0)

您是否在寻找每pid的点数?如果是这样,这是一个更简单的查询:

 SELECT pid, SUM(location like '%a%') as As,
        SUM(location like '%b%') as Bs,
        SUM(location like '%c%') as Cs
 FROM db.t
 WHERE (date > @dt)
 GROUP BY pid