advseo32
advseo32

Reputation: 401

Merge two columns from different tables in same column

I want to merge two columns in same columns from two different tables.

Using SQL, I have used join but not helpful any more.

Here is a detailed case :

I have a sellDetailTable and buyDetailTable

buy table

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

sell details table

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

I want to merge them like so

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

Upvotes: 0

Views: 293

Answers (1)

CompuChip
CompuChip

Reputation: 9232

You can use a UNION query:

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

and, if you want, insert that into your new table, e.g.

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

Upvotes: 1

Related Questions