..
The SQL language offers us a fairly simple way to combine, within the same SELECT statement, the results of two different tables.
To achieve this, it's time to use UNION all'opertore which will, in fact, the union of the results obtained by querying the two tables.
Note that in order to be used properly, it's time that the UNION operator:
SELECT name, stars, city, country FROM hotel_italia UNION SELECT name, stars, city, country FROM hotel_europaWith this query we get a complete listing of all hotels in the two tables:
| name | stars | city | nation |
| Hotel Cavour | 4 | Rome | EN |
| Hotel Miramare | 2 | Catholic | EN |
| Hotel Manzoni | 2 | Milan | EN |
| Hotel Espana | 3 | Madrid | ES |
| Hilton | 5 | London | UK |
| Hotel am Schlossgarten | 4 | Stuttgart | DE |
Suppose that the results of using UNION and many want them to be limited to specified number. How? Here's a solution:
SELECT * FROM (SELECT name, stars, city, country FROM hotel_italia UNION SELECT name, stars, city, country FROM hotel_europa) AS Hotel ORDER BY DESC LIMIT 10 starsEssentially we treat the results arising from the union of two or more tables as if they were the result of a single table, by doing so we can use standard sorting and limit.
| |
MS Access (Advanced)
Learn how to create and manage databases quickly and easily. Starting from 29 €. |
| |
MySQL (Course)
Management of open-source database. From 39 €. |
| |
SQL and Database (Course)
Create and manage relational databases. From 39 €. |