简体   繁体   中英

Mysql GROUP_CONCAT of first n rows

Is it possible to get comma separated value of first n (say 10 rows of a column) rows using Mysql?

I have a query to get data greater than CURDATE(). And it will return more than 100 rows of result. What I want is, GROUP_CONCAT the first 10 rows of result.

This is my query:

SELECT GROUP_CONCAT(user_id) AS userids
FROM user_tasks
WHERE due_date > CURDATE() LIMIT 10;

am getting entire rows. I need first 10 rows only

Thanks

Use subquery:

SELECT 
  GROUP_CONCAT(user_id) AS userids
FROM
  (SELECT 
    user_id
  FROM
    user_tasks
  WHERE due_date > CURDATE()
  LIMIT 10) AS users

You need to use a sub query to impose the limit, like this:

SELECT GROUP_CONCAT(sub_query.user_id) AS userids
FROM 
(
  SELECT user_id
  FROM user_tasks
  WHERE due_date > CURDATE() 
  LIMIT 10
) sub_query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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