elisa
elisa

Reputation: 509

Select rows with multiple same values using COUNT in MySQL

I have a table in MySQL database like this:

item    color   size
t-shirt blue    M
jumper  black   L
jumper  black   L
t-shirt blue    M

I want to select the rows that have the same value, so I get output like this:

item    color   size  total
t-shirt blue    M     2
jumper  black   L     2

This is what I've tried:

SELECT item, COUNT(*) as total FROM table_name GROUP BY color,size HAVING total >= 1

But, my query doesn't give the results I want. What is the query I can use to obtain the output?

Thank you so much.

Upvotes: 0

Views: 42

Answers (1)

Leni
Leni

Reputation: 683

You need to use count with a group by. Something similar to below:

select item, color, size,  count(*) as total from table_name group by item, color, size;

Upvotes: 1

Related Questions