intableの正しい使用法Order by
はunioned
select colname from table where <cond1>
union
select colname from table where <cond2> order by colname
上記のコードの説明は、2つのクエリが結合された後、列名に基づいて並べ替えるというものです。サーバーが最初のクエリと結合する前に、最初に下位のクエリを並べ替えることはありません。しかし、それに代わる方法があります。サブクエリでラップする必要があります。
SELECT newTable.colname
FROM
(
select colname, 1 as OrderThis from table where <cond1>
union
select colname, 2 as OrderThis from table where <cond2>
) newTable
Order by newTable.OrderThis, newTable.colname
私があなたの考えを正しく理解した場合union
、2つのクエリを正しくして正しい位置を維持する前に、最初に列を並べ替えます(最初のクエリ結果は上に残り、2番目のクエリ結果は最初のクエリの下に残ります)