我想合并来自两个不同表的相同列中的两列。
使用SQL,我已经使用了连接但不再有用。
这是一个详细的案例:
我有sellDetailTable
和buyDetailTable
buy
表
+--------------+--------------------+-----------+-----------+-----------------+
|buyId | supplier name | productId | QtyIn | price |
+--------------+--------------------+-----------+-----------+-----------------+
sell details
表
+--------+--------+----------+-------+------+
|sellId | client|productId |QtyOut | price|
+--------+--------+----------+-------+------+
我想合并它们
+---------------+------------------+----------+----------+-------+-----------+
|sellId or buyId|supplier or client|productId | QtyIn | QtyOut| price |
+---------------+------------------+----------+----------+-------+-----------+
答案 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>