简体   繁体   English

MySQL分组依据不带列名

[英]MySQL group by without column name

I have a table contains user infos. 我有一个包含用户信息的表。

mysql> desc accounts;
+-------------+------------+------+-----+---------+----------------+
| Field       | Type       | Null | Key | Default | Extra          |
+-------------+------------+------+-----+---------+----------------+
| cid         | int(11)    | NO   | PRI | NULL    | auto_increment |
| username    | text       | YES  |     | NULL    |                |
| password    | text       | YES  |     | NULL    |                |
| mysignature | text       | YES  |     | NULL    |                |
| is_admin    | varchar(5) | YES  |     | NULL    |                |
+-------------+------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

Normal group by statement always follows by one or more column names like below: 普通的group by语句始终跟随一个或多个列名,如下所示:

select * from accounts group by cid;

Just an example, in general group by works with Aggregate functions like sum() . 只是一个示例,通常group by使用诸如sum()类的Aggregate函数。

I have found some follows by an expression without column names which I cant understand: 我发现一些不符合我无法理解的列名的表达式:

mysql> select username from accounts group by now();
+----------+
| username |
+----------+
| admin    |
+----------+
1 row in set (0.00 sec)

I am new in MySQL. 我是MySQL的新手。 How does this query work ? 该查询如何工作? Thanks. 谢谢。

In fact it's expression. 实际上是表达。 It compares expression results instead of column values for the grouping. 它比较表达式结果而不是分组的列值。

now() function with the group by is not so effective. now()与group by的功能不是很有效。

It will always return the first row from the selected data. 它将始终从所选数据中返回第一行。

You always need to specify the column name in the group by clause in query. 您始终需要在查询的group by子句中指定列名。 Here are link of tutorial of group by. 这是分组依据的教程的链接 It specify that which aggregate function you can use with group by. 它指定可以与group by一起使用的聚合函数。

now() with group by return that will select the data from the query and match the first and it will be first row. now()与group by return一起,它将从查询中选择数据并与第一行匹配,并且它将是第一行。

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

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