Unions
Sometimes, you may not want to limit results but, rather, combine results from multiple queries. Rather than execute two different SELECT
statements in Python, you can pass the task to the server with UNION
. A UNION
is the combination of the results from two SELECT
statements into a single result set. Unlike JOIN
s (discussed in the next section), a UNION does do not combine the results side-by-side, but one after the other. So where the results from the first query end, the results from the second query begin.
The basic syntax of a UNION
is as follows:
(<SELECT statement 1>) UNION (<SELECT statement 2>);
Each SELECT
statement is discrete as they are neither related nor can they rely on each other's data. The number of columns returned by each SELECT
statement must be the same. Otherwise, MySQL will throw an error.
The data type of each column should be the same with respect to the columns of the other statement. If it is not, you can get strange results. Consider this UNION...