R
R
Romer1232020-10-07 03:43:23
SQL
Romer123, 2020-10-07 03:43:23

How to represent the name of the source table as data in a separate column in an SQL query?

There are 3 tables (a, b, c) with the same structure: product name and price. Products may be repeated or unique. Task: create a query to create a new table (product, table_num, price).
It should look something like:
product1 / a / 100
product1 / b / 120
product2 / b / 14.20
product2 / b / 14.20
product2 / c / 14.10
Nothing comes to mind at all. I would be grateful if someone could help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zexer, 2020-10-07
@zexer

CREATE TABLE ..
INSERT INTO table
SELECT product, 'a', 100
FROM table_A
UNION
SELECT product, 'b', 100
FROM table_B
UNION
SELECT product, 'c', 100
FROM table_C
UNION

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question