Mysql计数具有相同值的行数

时间:2021-04-15 08:00:28

I have in my table 3 rows. 2 of them with the same value (valueA) on field status and the 3-rd one with another value (valueB).

我在桌子上有3排。其中2个在字段状态上具有相同的值(valueA),而第3个在另一个值(valueB)上具有相同的值(valueA)。

What i want to do is to display a message to show me how many rows with the same value exists. Example below:

我想要做的是显示一条消息,向我显示存在多少具有相同值的行。示例如下:

EX: There are 2 status value in the database There are 1 status value in the database

EX:数据库中有2个状态值数据库中有1个状态值

How can i achive that? Thank you.

我怎么能做到这一点?谢谢。

2 个解决方案

#1


1  

You need group by and count

你需要分组和计数

 select your_column_name, count(*) from your_table 
 group by your_column_name;

in your case assuming status is th column name

在您的情况下,假设状态是列名称

select concat("there is " , count(*), "  status in your db") from your_table
group by status;

#2


0  

You have to use group by, you can learn more about GROUP BY here

您必须使用group by,您可以在此处了解有关GROUP BY的更多信息

In your case query would look something like:

在您的情况下,查询将类似于:

SELECT *, COUNT(*) as `total`
FROM <table>
GROUP BY `status `

#1


1  

You need group by and count

你需要分组和计数

 select your_column_name, count(*) from your_table 
 group by your_column_name;

in your case assuming status is th column name

在您的情况下,假设状态是列名称

select concat("there is " , count(*), "  status in your db") from your_table
group by status;

#2


0  

You have to use group by, you can learn more about GROUP BY here

您必须使用group by,您可以在此处了解有关GROUP BY的更多信息

In your case query would look something like:

在您的情况下,查询将类似于:

SELECT *, COUNT(*) as `total`
FROM <table>
GROUP BY `status `