mysql - 在 MySQL 5 中将 Limit 与 group_contact 一起使用
问题描述
大家早上好,我有一个问题:
我有以下示例 SQL:
group_concat( DISTINCT `mvu5877_anuncios_photos`.`image` ORDER BY `mvu5877_anuncios_photos`.`order` ASC SEPARATOR ',' ) AS `images`
在 SEPARATOR 中,我需要定义一个数量而不是带上所有项目。在 MariaDB 中,我可以在最后超过限制时做到这一点。例子:
group_concat( DISTINCT `mvu5877_anuncios_photos`.`image` ORDER BY `mvu5877_anuncios_photos`.`order` ASC SEPARATOR ',' LIMIT 4 ) AS `images
MySQL 5.7.32 中有更多语法错误。有什么建议吗?
解决方案
检查文档:GROUP_CONCAT()。在 GROUP_CONCAT() 调用中没有 LIMIT 关键字的语法。这是 MariaDB 特有的功能,在 MariaDB 10.3.3 中引入。
在 MySQL 5.7 中,您必须使用子查询来限制结果,然后在外部查询中使用 GROUP_CONCAT():
mysql> select group_concat( DISTINCT `image` ORDER BY `order` ASC SEPARATOR ',' ) AS `images`
from mytable;
+-----------------+
| images |
+-----------------+
| abc,def,ghi,jkl |
+-----------------+
mysql> select group_concat( DISTINCT `image` ORDER BY `order` ASC SEPARATOR ',' ) AS `images`
from (select * from mytable limit 2) as t;
+---------+
| images |
+---------+
| abc,def |
+---------+
推荐阅读
- postgresql - 如何在 Postgres 中使用创建索引来优化搜索
- c - 如何用 C 中的以下 3 位替换括号中的 3 位?
- laravel - laravel 中的 save() 方法仅将最后一条记录保存在 json 中
- ruby - 成功点击后更改页面:stale element reference: element is not attach to the page document
- bash - 为什么我不能在本地运行一个简单的程序?
- python - 如何在类内的函数内使用另一个文件中的方法
- r - 在 R 中,我如何可视化两列中的频率计数与跨时间的另一基线列的比较?
- rust - 你怎么能在你没有定义的东西上使用序列化和反序列化宏?
- bash - 检查多字命令输出是否以bash中的某个字符串开头
- java - BlobInput 的 Azure 函数绑定问题,没有任何错误