简体   繁体   English

在MySQL中使用COUNT选择具有多个相同值的行

[英]Select rows with multiple same values using COUNT in MySQL

I have a table in MySQL database like this: 我在MySQL数据库中有一个这样的表:

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 . 您需要将countgroup by一起使用。 Something similar to below: 类似于以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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