MySQL:此种查询结果,怎么仅保留第一条记录?
PHP中文网
PHP中文网 2017-04-17 14:41:39
0
3
930

select id, value from test;
查询结果如下
id value
yy 123
zz 234
zz 456

仅想保留id字段的第一条记录,在5.7以下版本中使用"select id, value from test group by id"即可得到如下结果:
yy 123
zz 234

但在MySQL 5.7中该如何得到所需结果呢?该语句在5.7中非法

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(3)
黄舟

Modify sql_mode in the MySQL configuration file and remove ONLY_FULL_GROUP_BY. If there is no sql_mode, add a line

sql_model=
PHPzhong

SELECT id, value FROM (SELECT id, value FROM test ORDER BY id ) AS b GROUP BY id

Ty80

A digression:
I am used to PostgreSQL and suddenly started using MySQL one day. I found that a non-key and non-aggregation-free SQL statement like the original poster can actually be established, and I was filled with fear.
Later I discovered that MySQL didn’t even use the same window functions that are commonplace in PostgreSQL, and I was completely speechless.
Okay, let’s provide something useful and implement the rank window function in MySQL:
http://stackoverflow.com/questions/3333665/rank-function-in-mysql

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template