Reputation: 999
I am trying to get data from multiple tables (all tables are identical)
Select column1,column2 From table1,table2
Is it a correct way to get this in MYSQL. (I am passing table names dynamically in the query)
Upvotes: 0
Views: 140
Reputation: 100205
Like:
SELECT table1.column1, table2.column2
FROM table1, table2
By fully-qualifying the column using the table, you won't run into conflicts when columns have the same names in the tables. As zessx states in his answer, an alternate way to do this is to alias the tables, which can make the query simpler with long table names / schema names.
Upvotes: 0
Reputation: 262834
Maybe this:
SELECT column1, column2
FROM table1
UNION ALL
SELECT column1, column2
FROM table2
But you cannot pass in table names dynamically. They must be known to the database in advance (so you need to construct the SQL in your program first).
Upvotes: 0
Reputation: 68820
The syntax you need is this one :
SELECT a.column1, b.column2
FROM table1 AS a, table2 AS b
AS
give your table an alias you can directly use in your request.
Upvotes: 1