2017-07-28 103 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

相關問題