Z
Z
z4p2015-09-03 09:06:47
SQL
z4p, 2015-09-03 09:06:47

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

1 answer(s)
H
heartdevil, 2015-09-03
@z4p

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 question

Ask a Question

731 491 924 answers to any question