1

我正在尝试创建没有子查询的 sql 查询,这是工作查询:

select * from customers 
join 
(select idContactOwner, count(*) contacts_count 
from contacts GROUP BY idContactOwner HAVING contacts_count>5) k 
where customers.id=k.idContactOwner;

这是我正在尝试的,但它不起作用:

select idContactOwner, count(*) counter from contacts A
group by idContactOwner having counter>5 join customers B
on A.idContactOwner=B.id;

这是sql小提琴:

http://sqlfiddle.com/#!2/724962/59

4

2 回答 2

3

group by必须紧随其后join

select A.idContactOwner, count(*) counter 
from contacts A
join customers B on A.idContactOwner = B.id
group by A.idContactOwner 
having counter > 5 
于 2013-09-08T16:18:44.373 回答
1
SELECT A.idContactOwner, B.whatEverFields, count(A.idContactOwner) AS counter
FROM contacts A, customers B
WHERE A.idContactOwner=B.id 
GROUP BY A.idContactOwner 
HAVING counter>5;

像这样的东西?

于 2013-09-08T16:20:26.833 回答