Sql server union but keep order

前端 未结 4 701
悲&欢浪女
悲&欢浪女 2020-12-05 09:58

Is there a way to union two tables, but keep the rows from the first table appearing first in the result set?

For example:

Table1

name                


        
4条回答
  •  情书的邮戳
    2020-12-05 10:23

    ;WITH cte as (
        SELECT name, surname, 1 as n FROM table1
        UNION ALL
        SELECT name, surname, 2 as n FROM table2
        UNION ALL
        SELECT name, surname, 3 as n FROM table3
    )
    SELECT name, surname
    FROM cte
    ORDER BY n;
    

提交回复
热议问题