简体   繁体   中英

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.

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;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM