Ordering by the order of values in a SQL IN() clause

前端 未结 13 1034
旧巷少年郎
旧巷少年郎 2020-11-22 04:12

I am wondering if there is away (possibly a better way) to order by the order of the values in an IN() clause.

The problem is that I have 2 queries, one that gets al

13条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 04:35

    I just tried to do this is MS SQL Server where we do not have FIELD():

    SELECT table1.id
    ... 
    INNER JOIN
        (VALUES (10,1),(3,2),(4,3),(5,4),(7,5),(8,6),(9,7),(2,8),(6,9),(5,10)
        ) AS X(id,sortorder)
            ON X.id = table1.id
        ORDER BY X.sortorder
    

    Note that I am allowing duplication too.

提交回复
热议问题