计数MySQL列中每一行的相同值?
要计算每一行的相同值,请使用COUNT(*)和GROUP BY子句。让我们首先创建一个表-
create table DemoTable1818(
Id int,
Name varchar(20)
);
使用插入命令在表中插入一些记录-
insert into DemoTable1818 values(10,'Chris');insert into DemoTable1818 values(11,'Chris');
insert into DemoTable1818 values(11,'Chris');
insert into DemoTable1818 values(12,'Chris');
insert into DemoTable1818 values(10,'Chris');
insert into DemoTable1818 values(10,'Chris');
使用select语句显示表中的所有记录-
select * from DemoTable1818;
这将产生以下输出-
+------+-------+| Id | Name |
+------+-------+
| 10 | Chris |
| 11 | Chris |
| 11 | Chris |
| 12 | Chris |
| 10 | Chris |
| 10 | Chris |
+------+-------+
6 rows in set (0.00 sec)
这是查询以计算列中每一行的相同值-
select Id,count(*) as TotalPerId from DemoTable1818 group by Id;
这将产生以下输出-
+------+------------+| Id | TotalPerId |
+------+------------+
| 10 | 3 |
| 11 | 2 |
| 12 | 1 |
+------+------------+
3 rows in set (0.00 sec)
以上是 计数MySQL列中每一行的相同值? 的全部内容, 来源链接: utcz.com/z/317065.html