Substaract来自两个不同表的两个列

时间:2019-09-06 04:34:33

标签: sql sqlite

我有2张桌子

    Table 1:
    +-----+------+-------+
    |  ID |  QTY |  Name |   
    |  1  |  14  |  Milk | 
    |  2  |  15  |  Egg  |
    |  3  |  44  | Apple |
    +-----+------+-------+

    Table 2:
    +-----+------+-------------+
    |  ID | QTY1 |  Location   |   
    |  1  |   5  |  Food shelf | 
    |  2  |   6  |  Food shelf |
    |  3  |   8  |  Food shelf |
    +-----+------+-------------+

我想从QTY中删除QTY1,加入两个表并显示如下

    Table 3:
    +-----+-------+-----+-----+-------------+-------+
    |  ID |  QTY  | QTY1| SOLD|  Location   | Name  |
    |  1  |   14  |  5  |  9  |  Food shelf | Milk  |
    |  2  |   15  |  6  |  9  |  Food shelf | Egg   |
    |  3  |   44  |  7  | 37  |  Food shelf | Apple |
    +-----+-------+-----+-----+-------------+-------+

有什么办法吗?

3 个答案:

答案 0 :(得分:0)

    select t1.ID, t1.QTY, t2.QTY as "QTY1", (t1.QTY-t2.QTY) as "SOLD", 
        t2.Location, t1.Name 
    from table1 t1 inner join table2 t2 on t1.ID = t2.ID;

答案 1 :(得分:0)

您可以这样做

SELECT t1.ID, t1.QTY, t2.QTY AS QTY1, (t1.QTY - t2.QTY) AS SOLD, t2.Location, t1.Name 
FROM table1 t1 inner join table2 t2 on t1.ID = t2.ID;

答案 2 :(得分:0)

您可以使用此查询...

   SELECT t1.id,t1.QTY,t2.QTY1,
    (t1.QTY-t2.QTY1) AS SOLD,
    t2.Location,t1.[NAME]
     FROM Table1 t1
    LEFT  OUTER JOIN Table2 t2
    ON t1.id=t2.id

输出:-

enter image description here