我正在尝试创建不带子查询的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

最佳答案

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;

像这样的?

关于mysql - 连接两个没有子查询的表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18685858/

10-15 11:01