我有用戶表,它有很多屬性。用戶表具有一對多關係。我儘量選擇具有一個屬性在屬性表一個很多查詢,其中許多有一個記錄
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
用戶但這並不似乎工作
我有用戶表,它有很多屬性。用戶表具有一對多關係。我儘量選擇具有一個屬性在屬性表一個很多查詢,其中許多有一個記錄
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
用戶但這並不似乎工作
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
你也可以把它作爲第二個請求:
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
我接受但我的問題似乎是相關的,當我在p.table上做一個地方,例如年(p.from)= 2017 – deroccha