If I use UNION to make multiple SELECTs from database, is considered one query? or multiple queries?
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
Is this considered a single query? or 2 queries?
(I am limited at number of queries per hour and I wan to optimise my code)
Thanks!
From the documentation :
The SQL UNION operator is used to combine the result sets of 2 or more
SELECT statements. It removes duplicate rows between the various
SELECT statements.
So your 2 select are combined into one query ;)
http://www.techonthenet.com/sql/union.php
This is one query as the others have pointed out.
I would be wary of this approach when coded into a project, unless the two tables have very similar data, for the following reasons..
A) The column count from the table1
select has to match the column count from the table2
select. You can get round this by adding NULL
columns.
B) You lose the column names from the second select.
C) It is not possible to tell which rows came from which table out of the box. You can get around this by adding a static type column to each select.
D) It is very easy to miss something and use the wrong data.
As an additional note. You are using UNION
which will drop dupes from the results, although it may be unlikely that you have the same data in both tables; if you are trying to reduce total queries it looks like you conceptually want a UNION ALL
.