MySQL: ORDER BY column DESC LIMIT (SELECT COUNT(*) FROM table);

user8555937 :

I apologize if this question is stupid. Code is pretty easy to understand.

Syntax error occurs at last line. How can I overcome it?

    SELECT id
    FROM m_users
    ORDER BY id DESC
    LIMIT (SELECT COUNT(*) FROM tmp_students);
Gordon Linoff :

You can use window functions:

SELECT u.id
FROM (SELECT u.*, ROW_NUMBER() OVER (ORDER BY id DESC) as seqnum
      FROM m_users u
     ) u
WHERE seqnum <= (SELECT COUNT(*) FROM tmp_students)
ORDER BY id DESC

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=372028&siteId=1