23. select u.id, u.email from users u where
( 1 =
(case when u.id not in
(select o.user_id from orders o INNER JOIN order_details od ON
o.id=od.order_id INNER JOIN items i ON od.item_id=i.id where
i.sti_type='phonecase')
then 0
when u.id in
(select o.user_id from orders o INNER JOIN order_details od ON
o.id=od.order_id INNER JOIN items i ON od.item_id=i.id where
i.sti_type='battery')
then 0 else 1 end)
);
24. select u.id, u.email from users u where
( 1 =
(case when u.id not in
(select o.user_id from orders o INNER JOIN order_details od ON
o.id=od.order_id INNER JOIN items i ON od.item_id=i.id where
i.sti_type='phonecase')
then 0
when u.id in
(select o.user_id from orders o INNER JOIN order_details od ON
o.id=od.order_id INNER JOIN items i ON od.item_id=i.id where
i.sti_type='battery')
then 0 else 1 end)
);
phonecaseを注文していないユーザーを取得
25. select u.id, u.email from users u where
( 1 =
(case when u.id not in
(select o.user_id from orders o INNER JOIN order_details od ON
o.id=od.order_id INNER JOIN items i ON od.item_id=i.id where
i.sti_type='phonecase')
then 0
when u.id in
(select o.user_id from orders o INNER JOIN order_details od ON
o.id=od.order_id INNER JOIN items i ON od.item_id=i.id where
i.sti_type='battery')
then 0 else 1 end)
);
batteryを注文したユーザーを取得
26. select u.id, u.email from users u where
( 1 =
(case when u.id not in
(select o.user_id from orders o INNER JOIN order_details od ON
o.id=od.order_id INNER JOIN items i ON od.item_id=i.id where
i.sti_type='phonecase')
then 0
when u.id in
(select o.user_id from orders o INNER JOIN order_details od ON
o.id=od.order_id INNER JOIN items i ON od.item_id=i.id where
i.sti_type='battery')
then 0 else 1 end)
);
28. select u.id, u.email from users u where u.id in (
select o.user_id from orders o
INNER JOIN order_details od ON o.id=od.order_id
INNER JOIN items i ON od.item_id=i.id
INNER JOIN users u ON o.user_id=u.id
WHERE i.sti_type='phonecase'
) AND u.id not in (
select o.user_id from orders o
INNER JOIN order_details od ON o.id=od.order_id
INNER JOIN items i ON od.item_id=i.id
INNER JOIN users u ON o.user_id=u.id
WHERE i.sti_type='battery'
)
29. select u.id, u.email from users u where u.id in (
select o.user_id from orders o
INNER JOIN order_details od ON o.id=od.order_id
INNER JOIN items i ON od.item_id=i.id
INNER JOIN users u ON o.user_id=u.id
WHERE i.sti_type='phonecase'
) AND u.id not in (
select o.user_id from orders o
INNER JOIN order_details od ON o.id=od.order_id
INNER JOIN items i ON od.item_id=i.id
INNER JOIN users u ON o.user_id=u.id
WHERE i.sti_type='battery'
)
30. select u.id, u.email from users u where u.id in (
select o.user_id from orders o
INNER JOIN order_details od ON o.id=od.order_id
INNER JOIN items i ON od.item_id=i.id
INNER JOIN users u ON o.user_id=u.id
WHERE i.sti_type='phonecase'
) AND u.id not in (
select o.user_id from orders o
INNER JOIN order_details od ON o.id=od.order_id
INNER JOIN items i ON od.item_id=i.id
INNER JOIN users u ON o.user_id=u.id
WHERE i.sti_type='battery'
)
33. select u.id, count(o.id) from orders o
INNER JOIN order_details od ON
o.id=od.order_id
INNER JOIN users u ON o.user_id=u.id
INNER JOIN items i ON od.item_id=i.id
where i.sti_type='walletphonecase'
group by u.id
having count(o.id) < 5
35. select u.email, count(order_filter.order_id) from
users u
LEFT OUTER JOIN
(select o.id oid, o.user_id uid from orders o
INNER JOIN order_details od ON
o.id=od.order_id
INNER JOIN items i ON od.item_id=i.id
where i.sti_type='walletphonecase'
) order_filter ON order_filter.user_id = u.id
group by u.id
having count(order_filter.oid) < 5
order by count(order_filter.oid)
36. select u.email, count(order_filter.order_id) from
users u
LEFT OUTER JOIN
(select o.id oid, o.user_id uid from orders o
INNER JOIN order_details od ON
o.id=od.order_id
INNER JOIN items i ON od.item_id=i.id
where i.sti_type='walletphonecase'
) order_filter ON order_filter.user_id = u.id
group by u.id
having count(order_filter.oid) < 5
order by count(order_filter.oid)
外部結合でJOINをして注文していないユーザーも取
得する
37. select u.email, count(order_filter.order_id) from
users u
LEFT OUTER JOIN
(select o.id oid, o.user_id uid from orders o
INNER JOIN order_details od ON
o.id=od.order_id
INNER JOIN items i ON od.item_id=i.id
where i.sti_type='walletphonecase'
) order_filter ON order_filter.user_id = u.id
group by u.id
having count(order_filter.oid) < 5
order by count(order_filter.oid)
count(*)だと0件の要素がカウント
されない