我有用户表,它有很多属性。用户表具有一对多关系。我尽量选择具有一个属性在属性表一个很多查询,其中许多有一个记录
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