gpt4 book ai didi

mysql - 在 WHERE 子句中使用计数值

转载 作者:行者123 更新时间:2023-11-29 13:15:42 25 4
gpt4 key购买 nike

我正在尝试使用 COUNT 的结果来限制结果。例如,我只想显示计数 (matching_img_count) 大于/等于 2 的行。

SELECT tel_number.number, COUNT(DISTINCT(i2.id_img)) as matching_img_count
FROM img_reference i
JOIN img_reference i2 ON i.id_img = i2.id_img
AND i2.id_tel=13777
JOIN tel_number ON i.id_tel = tel_number.id_tel
WHERE tel_number.id_tel != 13777
GROUP BY i.id_tel, i2.id_tel

我尝试了两件事:

HAVING matching_img_count >= 2

WHERE matching_img_count >= 2

不走运...任何建议将不胜感激。

最佳答案

试试这个:

SELECT tel_number.number, COUNT(DISTINCT(i2.id_img)) as matching_img_count
FROM img_reference i
JOIN img_reference i2
ON i.id_img = i2.id_img
JOIN tel_number
ON i.id_tel = tel_number.id_tel
WHERE tel_number.id_tel != 13777
AND i2.id_tel=13777
GROUP BY i.id_tel, i2.id_tel
HAVING COUNT(DISTINCT(i2.id_img)) >= 2

关于mysql - 在 WHERE 子句中使用计数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21491574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com