我想将表order
与表invoice
结合使用,因此我将ord_ref
和fah_nr
合并到同一个结果集中。有人可以帮我查询吗?
表顺序
ord_nr ord_dept ord_date ord_customer ord_anlegg ord_ref
17264 7 2005-11-16 52815 2005 55
18132 7 2008-08-30 58087 2008 527
表发票
fah_nr fah_customer fah_ordre fah_anlegg fah_avdeling
68903 52815 17264 2005 7
69616 53796 18132 2008 7
输出
Ord_nr fah_ordre fah_anlegg fah_nr ord_ref
17264 17264 2005 68903 55
18132 18132 2008 69616 527
答案 0 :(得分:1)
如果我理解了您的要求,那么您可以寻找简单的加入
SELECT o.Ord_nr,i.fah_ordre,fah_anlegg,fah_nr,ord_ref
FROM [Order] o
INNER JOIN Invoice i ON o.ord_nr = i.fah_ordre
答案 1 :(得分:0)
应该是一个非常简单的连接。只需选择所需的值,然后执行左连接或内连接
以下是INNER JOIN
的示例SELECT
Order.ord_nr AS ord_nr,
Invoice.fah_ordre AS fah_ordre,
Invoice.fah_anlegg AS fah_anlegg,
Invoice.fah_nr AS fah_nr,
Order.ord_ref AS ord_ref
FROM
Order
INNER JOIN
Invoice
ON
Order.ord_nr = Invoice.fah_ordre
以下是LEFT JOIN的示例
SELECT
Order.ord_nr AS ord_nr,
Invoice.fah_ordre AS fah_ordre,
Invoice.fah_anlegg AS fah_anlegg,
Invoice.fah_nr AS fah_nr,
Order.ord_ref AS ord_ref
FROM
Order
LEFT JOIN
Invoice
ON
Order.ord_nr = Invoice.fah_ordre
两者的区别在于,如果一个记录只存在于其中一个表中,那么LEFT JOIN仍然会给你一个结果,但INNER JOIN不会。
(编辑并删除了已弃用的旧隐式联接)