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 JOINs (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 ...

Get MySQL for Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.