我简化了以下mysql查询,该查询计算table1中的主键作为table2中的外键出现的次数。
SELECT table1.*, COUNT(table2.id) AS 'value'
FROM table1 INNER JOIN table2 ON table1.id = table2.id
WHERE table1.lastname LIKE 'smith'
WHERE table1.firstname LIKE 'john'
GROUP BY table2.id;
我想修改它以便它只选择超过某个阈值的计数,也就是说,如果john smith的id在table2中出现10次,而我的阈值是20,则不会选择他的记录。
答案 0 :(得分:0)
GROUP BY table2.id
HAVING COUNT(table2.id) >= @Threshold
答案 1 :(得分:0)
SELECT table1.*,table2.value as value
FROM table1,
(
SELECT table2.id, COUNT(table2.id) AS 'value'
FROM table1 INNER JOIN table2 ON table1.id = table2.id
WHERE table1.lastname LIKE 'smith' AND table1.firstname LIKE 'john'
GROUP BY table2.id
HAVING COUNT(table2.id)>=20
) table2
WHERE table1.id=table2.id;