1

I've a problem when using limit offset in postgresql. Although I specify limit offset values, it lists all data.

SELECT 
        array_to_json(
            array_agg(
                json_build_object(
                    'nickName', u.username,
                    'date', to_char(p.create_date, 'DD/MM/YYYY'),
                    'time', to_char(p.create_date, 'HH24:MM'),
                    'questionId', p.post_id,
                    'questionContent', p.content,
                    'status', CASE WHEN p.status = 0 THEN 'Waiting for approval' WHEN p.status = 1 THEN 'Approved' WHEN p.status = 0 THEN 'Reject' END,
                    'rejectReason', p.reject_reason,
                    'answerUrl', p.seo_url
                )
            )
        )
  INTO _posts
FROM posts p
  INNER JOIN users u ON u.user_id = p.user_id 
WHERE p.user_id = _user_id
LIMIT 5 OFFSET _page * 5;

Please review my code and tell me where the problem is.

1
  • Unrelated, but: create table new_table as select ... is preferred over the non-standard select ... into new_table Commented Feb 13, 2019 at 13:43

1 Answer 1

2

Without rewriting the whole query for you, json_agg may be more appropriate here.

SELECT json_agg(x) AS json_feed
FROM (
    SELECT
        column AS "columnName", ...
    FROM your_table
    LIMIT 5
) AS x
Sign up to request clarification or add additional context in comments.

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.