There are several identical tables in the database. How to make a request from all? Here is a request to choose from one:
mysql_query("select * from `table` where `dt` = '20161201'"); There are several identical tables in the database. How to make a request from all? Here is a request to choose from one:
mysql_query("select * from `table` where `dt` = '20161201'"); UNION - used to combine the results of two or more SQL queries into a single table consisting of similar rows. Both queries must return the same number of columns and compatible data types in the corresponding columns.
Example
<запрос1> UNION [ALL] <запрос2> UNION [ALL] <запрос3> .....; More detailed example
SELECT City, Country FROM Customers WHERE Country='Germany' UNION ALL SELECT City, Country FROM Suppliers WHERE Country='Germany' ORDER BY City; You can write UNION or UNION ALL .
The difference between the UNION and UNION ALL is that the first one throws out duplicate lines from the final result.
Addition from @teran from the comment: in a separate column, you can choose, for example, the name of the table from which the data came. a la
...., 'customers' as tbl FROM Customers if you suddenly need to know where the lines actually came from (which will make sense only in UNION ALL )
...., 'customers' as tbl FROM Customers , if you suddenly need to know where the lines actually came from (which will make sense only in the case of union all ) - teranCreate an over table using ENGINE = Merge. As a last resort uniting VIEW. Just at the bottom - UNION ALL directly in the request.
If you use just UNION (or its synonym UNION DISTINCT) in VIEW or direct request - get ready for a powerful overhead to sort and delete duplicates (well, that is, it will be slowed down).
* then overhead anyway) - Alexey ShimanskySource: https://ru.stackoverflow.com/questions/604281/
All Articles