Answer the question
In order to leave comments, you need to log in
How to choose the number of orders for different items?
There are tables (conditionally) of goods
items (id, name, price)
and orders
orders (id, item, date) .
How to select a list of products from the database with the number of orders for each of them? That is, how to get the number of joined orders for each product in the selection?
Answer the question
In order to leave comments, you need to log in
More or less like this.
SELECT i.id, i.name, COUNT(o.item) AS OrderCount
FROM items AS i
INNER JOIN orders AS o ON i.id = o.item
GROUP BY i.id, i.name
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question