how to sort order of LEFT JOIN in SQL query?

后端 未结 5 1656
悲哀的现实
悲哀的现实 2020-12-08 04:25

OK I tried googling for an answer like crazy, but I couldn\'t resolve this, so I hope someone will be able to help.

Let\'s say I have a table of users, very simple t

5条回答
  •  一生所求
    2020-12-08 04:46

    Older MySQL versions this is enough:

    SELECT
        `userName`,
        `carPrice`
    FROM `users`
    LEFT JOIN (SELECT * FROM `cars` ORDER BY `carPrice`) as `cars`
    ON cars.belongsToUser=users.id
    WHERE `id`='4'
    

    Nowdays, if you use MariaDB the subquery should be limited.

    SELECT
        `userName`,
        `carPrice`
    FROM `users`
    LEFT JOIN (SELECT * FROM `cars` ORDER BY `carPrice` LIMIT 18446744073709551615) as `cars`
    ON cars.belongsToUser=users.id
    WHERE `id`='4'
    

提交回复
热议问题