I have 3 tables:
At first I wanted to list all users with their respective count of orders like this:
That's easy, I'm doing a "select name, count(id) from users, orders where users.id = orders.userId group by name".
Now, I'd like to further filter this data to show only users with orders that have items with status = "unprocessed". I am not sure how to go about grouping data from 2 tables. In the end, I'm looking to get data like:
Thanks!
Use:
SELECT u.name,
COUNT(o.id) AS numOrders
FROM USERS u
JOIN ORDERS o ON o.userid = u.id
WHERE EXISTS(SELECT NULL
FROM ORDERS_ITEMS oi
WHERE oi.orderid = o.id
AND oi.status = 'unprocessed')
GROUP BY u.name
Using a JOIN to ORDERS_ITEMS
would require the use of DISTINCT in the COUNT:
SELECT u.name,
COUNT(DISTINCT o.id) AS numOrders
FROM USERS u
JOIN ORDERS o ON o.userid = u.id
JOIN ORDERS_ITEMS oi ON oi.orderid = o.id
AND oi.status = 'unprocessed'
GROUP BY u.name
...because of row duplication when there are more than one unprocessed item to an order...
select name, count(users.id) from users
left join orders
on users.id=orders.userId
left join orders_items
on orders.id=orders_items.orderId
where orders_items.status="unprocessed"
I'm testing this now, not quite sure if I have it right. Edit: seems good.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.