一个查询,其中多个查询有一个记录
One many query where many has one record
我的用户 table 有很多属性。用户 table 具有一对多关系。我尝试 select 在属性 table
中只有一个 属性 的用户
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
我的用户 table 有很多属性。用户 table 具有一对多关系。我尝试 select 在属性 table
中只有一个 属性 的用户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