简体   繁体   中英

Append SQL Tables, Union?

If I have the two following tables,

Table1

ItemNo  Desc    Order Number    Qty      S_Date     Location
AA       AA         AAA          A     AA/AA/AAAA     AAAA
BB       BB         BBB          B     BB/BB/BBBB     BBBB
CC       CC         CCC          C     CC/CC/CCCC     CCCC

Table 2

M_Order Item    M_Date      Total
XXX      X      XX/XX/XXXX   XX
YYY      Y      YY/YY/YYYY   YY

Can anyone advice me how to get the following table please.

Result Table

ItemNo  Desc    Order Number    Qty     S_Date    Location   M_Date     Total
AA       AA        AAA           A    AA/AA/AAAA    AAAA        
BB       BB        BBB           B    BB/BB/BBBB    BBBB        
CC       CC        CCC           C    CC/CC/CCCC    CCCC        
X       XXX                                                 XX/XX/XXXX    XX
Y       YYY                                                 YY/YY/YYYY    YY

Thanks

You can use a union and null the columns in each query that don't have values.

select
    ItemNo,
    Desc,
    OrderNumber,
    Qty,
    S_Date,
    Location,
    null as M_Date,
    null as Total
from Table_1
union
select
    Item,
    M_Order,
    null,
    null,
    null,
    null,
    M_Date,
    Total
from Table_2

You can do it using UNION as mentioned in the answer above, provided you have same data types for the columns you are combining

Check the following fiddle http://sqlfiddle.com/#!3/ef709/4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM