我有两张桌子:
CREATE TABLE stores (
stores_id varchar PRIMARY KEY,
owner_id varchar
);
CREATE TABLE sets (
sets_id varchar PRIMARY KEY,
stores_id varchar not null,
owner_id varchar not null,
item_id varchar not null,
);
如何发出显示商店中商品数量的请求? 由业主选择。
例如:
select
stores.*,
count(sets.item_id)
from stores
LEFT OUTER JOIN sets on stores.owner_id = sets.owner_id
where
stores.owner_id = 'e185775fc4f5'
GROUP BY stores.owner_id;
谢谢。
答案 0 :(得分:1)
这是你想要的吗?
select st.stores_id, count(se.item_id)
from stores st left join
sets se
on st.owner_id = se.owner_id
where st.owner_id = 'e185775fc4f5'
group by st.stores_id;
答案 1 :(得分:1)
我认为您需要加入商店和所有者,然后COUNT(DISTINCT item_id)
select
st.owner_id,
st.stores_id,
count(distinct se.item_id)
from stores st left join
sets se
on st.owner_id = se.owner_id
and st.stores_id = se.stores_id
group by st.owner_id, st.stores_id;
这将提供一个表格,显示所有者,商店,然后是商品数量