I have two tables with identical structure, identical userid
values but otherwise different data
I'm trying to do a query that will give me a combined list of results from both tables, on the basis of the userid
Table 1:
userid | time | location |
---|---|---|
18 | 1342 | New York |
Table 2:
userid | time | location |
---|---|---|
18 | 1800 | New Jersey |
And so I previously had two separate queries and would run my PHP based on the results of each separate query
SELECT * FROM table1 WHERE userid = 18
SELECT * FROM table2 WHERE userid = 18
But now, I would like to simply combine them all into one result set, since the structure between table1
and table2
are identical
So I tried the following query
SELECT * FROM (
(SELECT * FROM table1 WHERE userid = 18)
UNION ALL
(SELECT * FROM table2 WHERE userid = 18)
)
this results in the following error:
Every derived table must have its own alias
I guess I'm confused on why this would need an alias, since I just want the combined result.
I changed my query to:
SELECT * FROM (
(SELECT * FROM table1 as P WHERE userid = 18)
UNION ALL
(SELECT * FROM table2 as Q WHERE userid = 18)
) as R
And this does seem to work, but I'm confused as to why and if this is ultimately correct or if i'm something losing or altering my data by doing that