Alternative to using LIMIT keyword in a SubQuery in MYSQL

Answer suggested by Layke is wrong in my purview. Intention of using limit in subquery is so main query run on limited records fetched from subquery. And if we keep limit outside then it makes limit useless for subquery.

Since mysql doesn’t support yet limit in subquery, instead you can use JOIN as follows:

       
    SELECT * FROM test
    JOIN 
    (
        SELECT DISTINCT code_ver 
        FROM test 
        WHERE code_ver NOT LIKE '%DevBld%' 
        ORDER BY date DESC LIMIT 10
    ) d
    ON test.code_ver
    IN (d.code_ver)
    ORDER BY xyz;

Leave a Comment