I’ve got a few queries (20+) which all return the following three columns:
Building | Room | Other
all of which are text fields. I’d like to take all of those queries and combine them; so I’d like to see what the queries return as a whole.
For example, if I had a query SELECT Building, Room, Other FROM tblOne WHERE Room=10 along with SELECT Building, Room, Other FROM tblOne WHERE Building=20, how might I combine those two into one? Obviously this is a very simple example and my real queries are much more complicated, so writing them as 1 query is not feasible.
I’d like the above example to output:
Building | Room | Other
```````````````````````
20 | 1 | Some Stuff
20 | 10 | Some More
5 | 10 | Some Other
15 | 10 | Some Extra
20 | 5 | Some Text
All the ways I’ve tried have come up with the error that “Building, Room and Other could refer to more than one table” (aka it doesn’t want to combine them under one heading). What is the SQL syntax to fix this?
Combine these two Query with the help of UNION ALL && UNION like this
Notice
The UNION operator is used to combine the result-set of two or more SELECT statements.
Each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.
The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL.