SQL - retain ordering based on the query params

前端 未结 7 1920
借酒劲吻你
借酒劲吻你 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 09:00

    Insert the values into a temporary table and join your select to that.

    You can then do a natural order on your temporary table column.

    CREATE GLOBAL TEMPORARY TABLE sort_table (
      value       VARCHAR2(100),
      sort_order  NUMBER
    ) ON COMMIT DELETE ROWS;
    
    INSERT INTO sort_table VALUES ('B123',1);
    INSERT INTO sort_table VALUES ('B483',2);
    ... etc. ...
    
    select * from mytable
    inner join sort_table
    on mytable.mycolumn = sort_table.value
    order by sort_table.sort_order;
    

    To clear the temporary table, just COMMIT.

提交回复
热议问题