我有一个名为login的表,其中包含字段id和success(0或1)。
我只需要返回id,其中有多个条目成功,1比0(对于每个id)。
到目前为止,我只能查询#success,另一个查询#hits,另一个查询#hits> #fail
/*returns: id, #hits */
SELECT id, count(*) AS hits
FROM login u
WHERE u.success = 1
GROUP BY id;
/*returns: id, #fails*/
SELECT id, count(*) AS fails
FROM login u
WHERE u.success = 0
GROUP BY id;
/*returns id, #hits, #fails, #hits > #fails? */
SELECT id, SUM(success = 1) AS 'Hits' , SUM(success = 0) AS 'Fails', SUM(success = 1) > SUM(success = 0) as 'hits > fails?'
FROM login u
GROUP BY id;
只有一个字段,我无法进行任何直接比较。
有没有办法比较查询1和查询2的值?
我可以将查询3更改为仅返回SUM(success = 1)> SUM(成功= 0)?
提前谢谢。
答案 0 :(得分:1)
不确定,但您可以尝试这样的事情:
SELECT col
FROM tbl
GROUP BY col
HAVING SUM(col1) > SUM(col2)