Skip to content

Instantly share code, notes, and snippets.

@underhilllabs
Last active January 29, 2016 22:52
Show Gist options
  • Save underhilllabs/72058ad56c42171005ec to your computer and use it in GitHub Desktop.
Save underhilllabs/72058ad56c42171005ec to your computer and use it in GitHub Desktop.
Join two database tables with a UNION, but show which are which with asterisks

Join two database tables with a UNION, but show which are which with asterisks

select yes_no, '*' as 'write in', '*' as cid, uid from ucdelection_referendum_vote 
UNION 
select '*', writein_text, cid, uid from ucdelection_vote where uid = 1;
+--------+--------------------+------+-------+
| yes_no | write in           | cid  | uid   |
+--------+--------------------+------+-------+
| 0      | *                  | *    |     9 |
| 0      | *                  | *    |     1 |
| 0      | *                  | *    |    12 |
| 1      | *                  | *    |    12 |
| 1      | *                  | *    |     1 |
| 1      | *                  | *    |     9 |
| 1      | *                  | *    | 12003 |
| *      | NULL               | 13   |     1 |
| *      | NULL               | 14   |     1 |
| *      | NULL               | 22   |     1 |
| *      | NULL               | 19   |     1 |
| *      | NULL               | 21   |     1 |
| *      | NULL               | 20   |     1 |
| *      | Barbra O'Malley    | 0    |     1 |
| *      | Mabel Belladonna   | 0    |     1 |
| *      | Banny O'Bannion    | 0    |     1 |
| *      | Rebecca O'Trecka   | 0    |     1 |
| *      | NULL               | 34   |     1 |
| *      |                    | NULL |     1 |
| *      | wesley whale       | 0    |     1 |
| *      | NULL               | 15   |     1 |
| *      | willis             | 0    |     1 |
| *      | flo                | 0    |     1 |
| *      | wibur              | 0    |     1 |
| *      | NULL               | 12   |     1 |
| *      | flo fox            | 0    |     1 |
| *      | NULL               | 40   |     1 |
| *      | Bobby Bulldog      | 0    |     1 |
| *      | Pearl              | 0    |     1 |
| *      | Susie              | 0    |     1 |
| *      | Shimpy             | 0    |     1 |
| *      | Shempy             | 0    |     1 |
| *      | Gloria             | 0    |     1 |
| *      | NULL               | 65   |     1 |
| *      | NULL               | 58   |     1 |
| *      | NULL               | 66   |     1 |
| *      | NULL               | 59   |     1 |
| *      | NULL               | 44   |     1 |
| *      | Sherman Legrande   | 0    |     1 |
| *      | Dlwanrjy           | 0    |     1 |
| *      | Whoopy John        | 0    |     1 |
| *      | Seamus and Sherman | 0    |     1 |
| *      | NULL               | 45   |     1 |
+--------+--------------------+------+-------+
43 rows in set (0.01 sec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment