简体   繁体   中英

Not able to add limit in the GROUP_CONCAT(distinct(..))

SELECT 
    quiz.id AS quizId,
    quiz.name AS quizName,
    GROUP_CONCAT(distinct(quiz_attempt.quiz_attempt_status)
        ORDER BY quiz_attempt.id DESC limit 1) AS quizAttemptStatus
FROM
    course
        LEFT JOIN
    quiz ON quiz.id = course.quiz
        LEFT JOIN
    quiz_attempt ON quiz_attempt.quiz = quiz.id
WHERE
    (course.id = 'courseId'
        AND quiz_attempt.user = 'userId' )
GROUP BY quiz.id , quiz_attempt.id

When I am adding limit inside GROUP_CONCAT(distinct(... )), it is not working and I want latest attempt for each quiz in the course. How to do it?

try using row-number and then only grabbing first row-number?

ROW_NUMBER() OVER (
    [PARTITION BY partition_expression, ... ]
    ORDER BY sort_expression [ASC | DESC], ...
)

also see more info here: https://www.sqlservertutorial.net/sql-server-window-functions/sql-server-row_number-function/

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