簡體   English   中英

合並具有不同列號的兩個不同表的結果,使用相同的鍵對它們進行排序

[英]Merge results from two different tables with different column number, ORDER them by same key

我有兩張桌子:

Table A: ID Cars Planes Num
          1    3      5   2
          2    8      44  1
          3    7      23  6
          4    6      2   7

Table B: ID Horses Dogs Cats Elefants Num
          1    3      5   2      3     3  
          2    8      44  1     22     4
          3    7      23  4     14     8
          4    6      2   3     15     5

我需要做的是:我需要從兩個表中獲取所有結果並按“Num”列對它們進行排序,其中“數字”實際上對於兩行的每個結果都是唯一的。

甚至可以“合並”這兩個表並按“num”排序它們,還是我只需要將每個表單獨排序並進行兩個循環檢查表格之間的下一個數字跳轉?

謝謝

你可以像UNION那樣合並它們。

嘗試這個:

select num from(
    select num from table1
    union all
    select num from table2
    )t
order by num asc

在這里演示

編輯:

select id ,Cars,Planes, Horses,Dogs,Cats,Elefants,num from(
    select id ,Cars,Planes,'No horses' Horses,'No dogs' Dogs,'No cats' Cats,'No elefants' Elefants,num from table1
union all
select id,'No cars' Cars,'No planes' Planes,Horses,dogs,Cats,Elefants, num from table2
 )t
 order by num asc;

與其他列的DEmo

SELECT NUM FROM TABLEA
UNION ALL
SELECT NUM FROM TABLEB
ORDER BY 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM