Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Data from two tables with identical columns

Tags:

mysql

I have two tables that have the same structure; one contains permanaent data, and one is cleared and reset on a regular basis.

I need to make the same select statement work on both as if they were just one table

This is what I tried:

SELECT * FROM a,b WHERE 1;

Where a and b have the same structure;

like image 907
Issac Kelly Avatar asked Feb 26 '09 02:02

Issac Kelly


2 Answers

You may be looking at using a UNION in you query:

Select * from a
UNION
Select * from b

Note: It is better practice to qualify your column names instead of using the * reference. This would also make the query still useful if your two tables underwent schema changes but you still wanted to pull back all the data the two tables had in common.

like image 169
TheTXI Avatar answered Nov 09 '22 13:11

TheTXI


So you want one set of results that contains the contents of both tables? If so then you'll need to do something like this:

select a.col1, a.col2 from a where...
UNION
select b.col1, b.col2 from b where...

mysql union syntax

like image 36
Kevin Tighe Avatar answered Nov 09 '22 15:11

Kevin Tighe