如何折叠这个嵌套的MySQL查询?

omhiaaxx  于 2022-11-28  发布在  Mysql
关注(0)|答案(1)|浏览(124)
SELECT num, COUNT(num) AS amt
FROM table
WHERE num IN (
    SELECT num
    FROM table
    WHERE id IN (1,2,3))
GROUP BY num
HAVING amt > 1

还有,我怎么才能
“777”=〉“2”
..而不是
'数字' =〉'777'
'金额' =〉'2'
结果呢?
谢谢

dm7nw8vv

dm7nw8vv1#

不能对聚合列执行where,必须对这些列使用having

SELECT t1.num, 
       COUNT(t1.num) AS amt 
FROM `table` t1
INNER JOIN `table` t2 ON (t1.num = t2.num)
WHERE t2.id IN (1,2,3)
GROUP BY t1.num
HAVING amt > 1

相关问题