简体   繁体   English

在 MySQL 5 中使用限制与 group_contact

[英]Using Limit with group_contact in MySQL 5

Good morning guys, I have a question:各位早上好,我有一个问题:

I have the following sample SQL:我有以下样品 SQL:

group_concat( DISTINCT `mvu5877_anuncios_photos`.`image` ORDER BY `mvu5877_anuncios_photos`.`order` ASC SEPARATOR ',' ) AS `images`

In SEPARATOR I need to define a quantity instead of bringing all the items.在 SEPARATOR 中,我需要定义一个数量而不是带上所有项目。 In MariaDB I can do this just past the Limit at the end.在 MariaDB 中,我可以在最后超过限制时执行此操作。 Example:例子:

group_concat( DISTINCT `mvu5877_anuncios_photos`.`image` ORDER BY `mvu5877_anuncios_photos`.`order` ASC SEPARATOR ',' LIMIT 4 ) AS `images

More in MySQL 5.7.32 the syntax error. MySQL 5.7.32 中的更多语法错误。 Any suggestion?有什么建议吗?

Check the documentation: GROUP_CONCAT() .检查文档: GROUP_CONCAT() There is no syntax for a LIMIT keyword inside a GROUP_CONCAT() call.在 GROUP_CONCAT() 调用中没有 LIMIT 关键字的语法。 This is a feature specific to MariaDB, introduced in MariaDB 10.3.3 .这是 MariaDB 特有的功能,在 MariaDB 10.3.3 中引入

In MySQL 5.7, you would have to use a subquery to limit the results, then use GROUP_CONCAT() in the outer query:在 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 |
+---------+

You can use SUBSTRING_INDEX() so that you get the top 4 values in the results:您可以使用SUBSTRING_INDEX()以便获得结果中的前 4 个值:

SUBSTRING_INDEX(
  GROUP_CONCAT(DISTINCT `mvu5877_anuncios_photos`.`image` ORDER BY `mvu5877_anuncios_photos`.`order`),
  ',',
  4
)

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

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