spaceman
spaceman

Reputation: 649

Select Distinct and Count on the same query MySQL

I have a table products, and each product has a specific brand.

What I want to do is to count the times a specific brand has appeared and, together, show the unique product it has.

For example:

Table products

ID, ProductName, Brand
1, TV, Samsung
2, TV, Sony
3, TV, Samsung
4, Phone, Sony

The query should result:

ProductName, Brand, Count
TV, Samsung, 2
TV, Sony, 1
Phone, Sony, 1

Can you help?

Sorry I'm writing on a phone and on the bus that's why I didn't specify but please feel free to ask if you didn't understand.

Upvotes: 0

Views: 40

Answers (2)

greendemiurge
greendemiurge

Reputation: 509

Looks like you are looking for GROUP BY: https://www.w3schools.com/sql/sql_groupby.asp

Give this a try:

 SELECT ProductName, Brand, count(id) AS Count FROM products GROUP BY ProductName, Brand

Upvotes: 2

Tik
Tik

Reputation: 882

Easy with Group by

SELECT productName, Brand, COUNT(*)
FROM yourdb
GROUP BY productName, Brand

Upvotes: 3

Related Questions