简体   繁体   English

mysql计算具有特定列的行

[英]mysql count rows with a specific column

I have a table like this我有一张这样的桌子

Sr Name先生姓名
1 A 1 安
2 B 2 乙
3 C 3 C
4 C 4 C
5 C 5℃
6 E 6 E
7 A 7A
8 A 8A
9 A 9A
10 E 10 E
11 B 11 乙
12 B 12 乙

I need output like this我需要这样的输出
A = 4 Times A = 4 次
B = 3 Times B = 3 次
C = 3 Times C = 3 次
E = 2 Times E = 2 次

How can I achieve this?我怎样才能做到这一点?

Thanks in advance提前致谢

SELECT Name, COUNT(Sr) FROM myTable GROUP BY Name ORDER BY Name ASC;

You may want to use:您可能想要使用:

SELECT   name, CONCAT(COUNT(*), ' Times') number
FROM     your_table
GROUP BY name
ORDER BY name;

Test case:测试用例:

CREATE TABLE your_table (sr int, name varchar(50));
INSERT INTO your_table VALUES(1, 'A');
INSERT INTO your_table VALUES(2, 'B');
INSERT INTO your_table VALUES(3, 'C');
INSERT INTO your_table VALUES(4, 'C');
INSERT INTO your_table VALUES(5, 'C');
INSERT INTO your_table VALUES(6, 'E');
INSERT INTO your_table VALUES(7, 'A');
INSERT INTO your_table VALUES(8, 'A');
INSERT INTO your_table VALUES(9, 'A');
INSERT INTO your_table VALUES(10, 'E');
INSERT INTO your_table VALUES(11, 'B');
INSERT INTO your_table VALUES(12, 'B');

Result:结果:

+------+---------+
| name | number  |
+------+---------+
| A    | 4 Times |
| B    | 3 Times |
| C    | 3 Times |
| E    | 2 Times |
+------+---------+
4 rows in set (0.00 sec)

Or if you prefer a one column result set, you may want to do:或者,如果您更喜欢一列结果集,您可能需要执行以下操作:

SELECT   CONCAT(name, ' = ', COUNT(*), ' Times') result
FROM     your_table
GROUP BY name
ORDER BY name;

Result:结果:

+-------------+
| result      |
+-------------+
| A = 4 Times |
| B = 3 Times |
| C = 3 Times |
| E = 2 Times |
+-------------+
4 rows in set (0.00 sec)
select count(Name) , Name
from yourtable
group by Name
order by name

I'll offer an alternative syntax from the others that will work just the same.我将提供与其他语法相同的替代语法。

SQL: ( Demo ) SQL:(演示

SELECT Name, COUNT(1)
FROM Names
GROUP BY 1 ASC

Result set:结果集:

| Name | COUNT(1) |
| ---- | -------- |
| A    | 4        |
| B    | 3        |
| C    | 3        |
| E    | 2        |
  1. Using group by will effectively return only rows with unique Name values.使用 group by 将有效地仅返回具有唯一Name值的行。
  2. The COUNT(1) will count the number of rows in each aggregate generated by using GROUP BY on the first column in the SELECT -- think of the aggregate data as a cluster of rows which must be "flattened somehow" by a MySQL function before it can be introduced to the result set. COUNT(1)将计算通过在SELECT第一列上使用GROUP BY生成的每个聚合中的行数——将聚合数据视为一组行,在此之前必须由 MySQL 函数“以某种方式展平”它可以被引入到结果集中。
  3. GROUP BY will happily receive the sorting order declaration after the targeted column, so the ORDER BY clause can be omitted. GROUP BY会很高兴地收到目标列之后的排序顺序声明,因此可以省略ORDER BY子句。

ps I am not sure if you actually need a single column of data which is pre-formatted as written in your question OR if this was just your way of expressing your desired result set and you actually want two columns of data without the bloat of plain text. ps我不确定你是否真的需要一列数据,它是按照你的问题中所写的预先格式化的,或者这是否只是你表达所需结果集的方式,而你实际上想要两列数据而没有普通的膨胀文本。

If you need the pre-formatted text version, then the magic 1 will not be suitable -- the column will need to be referenced explicitly.如果您需要预先格式化的文本版本,那么magic 1将不适合——需要明确引用该列。

SELECT CONCAT(Name, ' = ', COUNT(Name), ' Times')
FROM Names
GROUP BY Name DESC

Personally, I would not use CONCAT() for this task unless , say, you are generating a json reponse for an ajax call (passing the result set to a different layer).就个人而言,我不会将CONCAT()用于此任务,除非您正在为 ajax 调用生成 json 响应(将结果集传递到不同的层)。 In other words, you are instantly wrapping the full result set in json_encode() then back to javascript where you are passing it directly to a Google api for graphic representation.换句话说,您立即将完整的结果集包装在json_encode()然后返回到 javascript,在那里您将其直接传递给 Google api 以进行图形表示。

Excerpt from the PHP documentation -- specifically mysqli's fetch_all() :摘自 PHP 文档——特别是 mysqli 的fetch_all()

[...] should only be used in those situations where the fetched result set will be sent to another layer for processing. [...] 应该只在获取的结果集将被发送到另一层进行处理的情况下使用。

Otherwise, if you are going to manually iterate over the rows at any point in the flow, the inclusion of static text should be done at that point in your application.否则,如果您要在流程中的任何点手动迭代行,则应在应用程序中的那个点包含静态文本。 This will keep your result set lean & clean and make it is easier for you to maintain your application.这将使您的结果集保持精简和干净,并使您更容易维护您的应用程序。

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

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