2017-07-28 102 views
0

我有用户表,它有很多属性。用户表具有一对多关系。我尽量选择具有一个属性在属性表一个很多查询,其中许多有一个记录

select *, count(p.account_id) as c 
form accounts as a 
left join properties as p on a.account_id = p.account_id 
group by p.account_id 
having c = 1 

用户但这并不似乎工作

回答

0
SELECT *, count(*) as num from 
accounts as a INNER JOIN properties as p 
ON a.account_id = p.account_id 
group by p.account_id 
having num=1 
+0

我接受但我的问题似乎是相关的,当我在p.table上做一个地方,例如年(p.from)= 2017 – deroccha

1

你也可以把它作为第二个请求:

select * 
from (
    SELECT *, p.account_id , count(*) as num 
    from accounts as a 
    INNER JOIN properties as p ON a.account_id = p.account_id 
    group by p.account_id 
) query 
where query.num = 1 
+0

感谢您对我的产生的反馈重复account_id – deroccha

+0

其实你给一个好主意智慧这是为了解决我的问题。谢谢 – deroccha

+0

然后我很高兴它有帮助。你找到了解决方案? – JBO

相关问题