i have severals tables, named like these:
table_2012_12
table_2012_10
table_2012_07
both with the same structure:
id
order_id
account_id
i know i have to perform these queries:
SELECT * FROM table_2012_12 where id=1
SELECT * FROM table_2012_12 where id=5
SELECT * FROM table_2012_10 where id=1
SELECT * FROM table_2012_07 where id=22
What’s the best (in performances) method to use a single query?
The
UNIONoperator does that. See: http://dev.mysql.com/doc/refman/5.6/en/union.htmlUsually, you should not just write
UNIONbutUNION ALL.UNIONhas the effect of joining the query results, as well as filtering out any duplicate rows.UNION ALLwill simply merge the query results, and keep all rows. This is much more efficient