如何从两个表编写存储过程

时间:2014-11-14 00:50:16

标签: sql

我有2张牌桌 - t1t2。从t1开始,我想选择列order_no& order_Id在表格t2中有两列 - order_noinv_no

我想选择并显示表格order_no中的order_idt1,以及表order_no中的inv_no是否有t2 ,我必须将其附加到order_no这样=> ord1002_inv045 ..

1 个答案:

答案 0 :(得分:0)

试试这个。

SELECT OrderNo = t1.order_no
    , OrderId = t1.order_Id
    , OrderNoWithInvoiceId = 
      CASE 
        WHEN t2.inv_no IS NULL 
            THEN t1.order_no 
        ELSE t1.order_no + '_' + t2.inv_no 
      END 
FROM t1 INNER JOIN t2 ON t1.order_no = t2.order_no