SQL - retain ordering based on the query params

前端 未结 7 1939
借酒劲吻你
借酒劲吻你 2021-02-10 08:34

I\'m trying to perform a SELECT with an IN clause and I would like to be able to have the results returned in the same order as the elements in my list

7条回答
  •  渐次进展
    2021-02-10 08:49

    I'm not a Python guy, but here's how I'd do it in PHP and I hope you get the idea.

    1. Build a string like this:

      $str = "('B123', 'B483', 'B100', 'B932', ...)";

      For the above, you can use a for loop or something to build a really long string.

    2. Insert the string in the query like this:

    $MyQuery = "SELECT * FROM orders WHERE order_no IN $str
    ORDER BY $str";
    

    Like I said, this is a PHP example, but I believe you get the idea.

提交回复
热议问题