合并同一列中不同表的两列

时间:2013-11-02 10:54:08

标签: sql merge

我想合并来自两个不同表的相同列中的两列。

使用SQL,我已经使用了连接但不再有用。

这是一个详细的案例:

我有sellDetailTablebuyDetailTable

buy

+--------------+--------------------+-----------+-----------+-----------------+
|buyId         | supplier  name     | productId | QtyIn     |  price          |
+--------------+--------------------+-----------+-----------+-----------------+

sell details

+--------+--------+----------+-------+------+
|sellId  |  client|productId |QtyOut | price|
+--------+--------+----------+-------+------+

我想合并它们

+---------------+------------------+----------+----------+-------+-----------+
|sellId or buyId|supplier or client|productId | QtyIn    | QtyOut| price     |
+---------------+------------------+----------+----------+-------+-----------+

1 个答案:

答案 0 :(得分:1)

您可以使用UNION查询:

SELECT buyId, suppliername, productId, QtyIn, 0, price
UNION
SELECT sellId, client, productId, 0, QtyOut, price

并且,如果您愿意,请将其插入新表格,例如

INSERT INTO sellBuyTable(buyOrSellId, supplierOrclient, productId, qtyIn, qtyOut, price)
<query above>