8
set countt = PAGE_ * 5 - 5;

        set @data = (select JSON_ARRAYAGG(JSON_OBJECT('id', room_participants.id, 'isAdmin', room_participants.isAdmin, 'userId', room_participants.userId, 'joinTime', room_participants.joinTime, 'leftTime', room_participants.leftTime, 'status', room_participants.status, 'kickTime', room_participants.kickTime, 'displayName', users.displayName, 'phone', users.phone, 'created', created))
                        from room_participants 
                        LEFT JOIN users ON room_participants.userId = users.id 
                        WHERE room_participants.roomId = ROOMID_ LIMIT 5 OFFSET countt);

I need the above query to display only limit to 5 data in json_arrayagg format, but its return me the whole data without limit it to 5. What is the problem with my query?

3 Answers 3

3

I don't know the exact reason why your code isn't working, but one workarond would be to apply LIMIT to a subquery, and then form the JSON based on that:

SET countt = PAGE_ * 5 - 5;

SET @data = (
    SELECT JSON_ARRAYAGG(JSON_OBJECT('id', id, 'isAdmin', isAdmin, 'userId', userId, 'joinTime', joinTime, 'leftTime', leftTime, 'status', status, 'kickTime', kickTime, 'displayName', displayName, 'phone', phone, 'created', created))
    FROM
    (
        SELECT rp.id, rp.isAdmin, rp.userId, rp.joinTime, rp.leftTime, rp.status,
               rp.kickTime, u.displayName, u.phone, created
        FROM room_participants rp
        LEFT JOIN users u ON rp.userId = u.id 
        WHERE rp.roomId = ROOMID_
        LIMIT 5 OFFSET countt
    ) t
);

Note that using LIMIT without ORDER BY is generally not a well-defined thing. You should also ideally add an ORDER BY clause to your subquery.

Sign up to request clarification or add additional context in comments.

Comments

2

you need to use subquery first to limit your result before aggregating.

set countt = PAGE_ * 5 - 5;

set @data = (select JSON_ARRAYAGG(JSON_OBJECT('id', id, 'isAdmin'
                , isAdmin, 'userId', userId, 'joinTime'
                , joinTime, 'leftTime', leftTime
                , 'status', status, 'kickTime', kickTime
                , 'displayName', displayName, 'phone', phone, 'created', t.dCreated)
            )
            from 
            (select t1.*, t2.*, t1.created as dCreated from room_participants t1 
                left join users t2 on t1.userId = t2.id 
                where t1.roomId = ROOMID_ limit 5 offset countt) t);

1 Comment

did this work? I see a LIMIT inside a subquery, which doesn't work in MySQL according to the official docs
0

I had the same problem here is my take on it

with cte as (select JSON_OBJECT('id', room_participants.id, 'isAdmin', room_participants.isAdmin, 'userId',
                            room_participants.userId, 'joinTime', room_participants.joinTime, 'leftTime',
                            room_participants.leftTime, 'status', room_participants.status, 'kickTime',
                            room_participants.kickTime, 'displayName', users.displayName, 'phone', users.phone,
                            'created', created) single_row
         from room_participants
                  LEFT JOIN users
                            ON room_participants.userId = users.id
         WHERE room_participants.roomId = ROOMID_
         LIMIT 5 OFFSET countt) select json_arrayagg(single_row) from cte

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.