I'm using UNION
to get all names in different tables.
my tables has about 10000 rows all together.
but the query returns 468 rows!!
My query is:
SELECT name FROM `shopping`
UNION
SELECT name FROM stores
UNION
SELECT name FROM concert
UNION
SELECT val AS name FROM event
UNION
SELECT name FROM fastfood
Where is the problem?
Here is the query to count on the union query. mysql> select count(*) as UnionCount from -> ( -> select distinct UserId from union_Table1 -> union -> select distinct UserId from union_Table2 -> )tbl1; The following is the output displaying the count.
Because the UNION ALL operator does not remove duplicate rows, it runs faster than the UNION operator.
The difference between Union and Union all is that Union all will not eliminate duplicate rows, instead it just pulls all the rows from all the tables fitting your query specifics and combines them into a table. A UNION statement effectively does a SELECT DISTINCT on the results set.
The only difference between Union and Union All is that Union extracts the rows that are being specified in the query while Union All extracts all the rows including the duplicates (repeated values) from both the queries.
UNION removes duplicate values. You probably want UNION ALL instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With