When I do "WHERE IN" on MySQL, the order is sorted on its own

Asked 2 years ago, Updated 2 years ago, 31 views

SELECT
    u.*

FROM
    users u

WHERE
    u.id IN (5, 4, 1, 2, 3)

If you do it, the result is The results of the arrangement of 1, 2, 3, 4, and 5 come out, so can't we make it come out as we put it in?

mysql

2022-09-22 19:52

1 Answers

It's a self-answer.

SELECT
    u.*

FROM
    users u

WHERE
    u.id IN (5, 4, 1, 2, 3)

ORDER BY
    FIELD(u.id, 5, 4, 1, 2, 3)


2022-09-22 19:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.