I have a N:M relation that connects customers and purchase Customer_id,Purchase_id
. I would like to know how many purchases each specific user made, from one query.
I tried this:
SELECT DISTINCT(Customer_id) AS ID,COUNT(Purchase_id) AS P FROM CustomerPruchases;
but it only gives me one row for the first customer in the DB. I want it for all.
If you group then aggregate functions like count()
apply to each group and not to the complete table
SELECT Customer_id as ID,
COUNT(Purchase_id) AS P_COUNT
FROM CustomerPruchases
group by ID
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.