Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select same columns from multiple tables

Tags:

mysql

I have a MySQL database with multiple tables and those tables contain multiple columns that are equal.

For example:

table1 contains productid, price, and a couple of columns specific for that table.

table2 contains productid, price, and a couple of different columns (not in table1)

table3 also has productid, price and more unique columns, etc etc.

Is it possible to select productid and price from all three tables in one query and get the result in one output? One way would be to select into some temporary table, but is there an easier/better/nicer way?

like image 881
ajsnigrutin Avatar asked Oct 25 '17 09:10

ajsnigrutin


1 Answers

using the union :

    select productid,price from table1
union select productid,price from table2
union select productid,price from table3
like image 184
Y.Ido Avatar answered Oct 15 '22 15:10

Y.Ido