{"id":80,"date":"2013-10-03T15:03:41","date_gmt":"2013-10-03T19:03:41","guid":{"rendered":"http:\/\/access-diva.com\/blog\/?p=80"},"modified":"2018-07-04T19:35:09","modified_gmt":"2018-07-04T23:35:09","slug":"union-queries","status":"publish","type":"post","link":"https:\/\/access-diva.com\/blog\/?p=80","title":{"rendered":"UNION Queries"},"content":{"rendered":"<p>A UNION query takes data from two (or more) tables and\/or queries and makes it appear as if it is from one source, i.e&#8230;<\/p>\n<p>SELECT FieldName, FieldName<br \/>\nFROM 1stTableOrQueryName<br \/>\nUNION SELECT FieldName, FieldName<br \/>\nFROM 2ndTableOrQueryName<br \/>\nUNION SELECT FieldName, FieldName<br \/>\nFROM 3ndTableOrQueryName;<\/p>\n<p><strong>A few points\u2026<\/strong><\/p>\n<p><strong>1.<\/strong> UNION queries are <span style=\"color: #cc0033;\"><strong>READ<\/strong> <strong>ONLY<\/strong><span style=\"color: #000000;\">, no edits allowed!<\/span><\/span><\/p>\n<p><strong>2.<\/strong> In the above rendition there can be no duplication of the rows returned. If any duplicate rows are specifically required to be returned, a <strong>UNION ALL<\/strong> operation should be used.\u00a0 However, I rarely find a reason to want duplicate rows in a UNION query.<\/p>\n<p><strong>3.<\/strong> These queries <strong>can<\/strong> *run* slowly so use them sparingly.<\/p>\n<p><strong>4.<\/strong> The <strong>Column Count<\/strong> must be the same for each query.\u00a0 If they do not match up you can use <strong>Null <\/strong>in place of a field name as a placeholder, i.e&#8230;<\/p>\n<p>SELECT FieldName, FieldName<br \/>\nFROM 1stTableOrQueryName<br \/>\nUNION SELECT FieldName, <strong>Null<\/strong><br \/>\nFROM 2ndTableOrQueryName<br \/>\nUNION SELECT <strong>Null<\/strong>, FieldName<br \/>\nFROM 3ndTableOrQueryName;<\/p>\n<p><strong>5.<\/strong> You can specify <strong>Column Names<\/strong>&#8230;<\/p>\n<div id=\"attachment_86\" style=\"width: 342px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-86\" class=\"wp-image-86 size-full\" src=\"https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2013\/09\/FirstNameLastName.png\" alt=\"Specify Column Names\" width=\"332\" height=\"125\" srcset=\"https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2013\/09\/FirstNameLastName.png 332w, https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2013\/09\/FirstNameLastName-300x112.png 300w, https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2013\/09\/FirstNameLastName-150x56.png 150w\" sizes=\"auto, (max-width: 332px) 100vw, 332px\" \/><p id=\"caption-attachment-86\" class=\"wp-caption-text\">Specify Column Names<\/p><\/div>\n<p>SELECT FieldName <strong>AS FirstName<\/strong>, FieldName <strong>AS LastName, <\/strong>FieldName<strong>\u00a0AS TypeID<\/strong><br \/>\nFROM 1stTableOrQueryName<br \/>\nUNION SELECT FieldName, FieldName, FieldName<br \/>\nFROM 2ndTableOrQueryName<br \/>\nUNION SELECT FieldName, FieldName, FieldName<br \/>\nFROM 3ndTableOrQueryName;<\/p>\n<p>You only need to do this for the first query as the following queries will adopt whatever Column Name(s) is specified in the first query.<\/p>\n<p><strong>6.<\/strong> You can use <strong>ORDER BY<\/strong> in a UNION query, i.e&#8230;<\/p>\n<div id=\"attachment_87\" style=\"width: 362px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-87\" class=\"wp-image-87 size-full\" src=\"https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2013\/09\/OrderBy.png\" alt=\"ORDER BY LastName\" width=\"352\" height=\"211\" srcset=\"https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2013\/09\/OrderBy.png 352w, https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2013\/09\/OrderBy-300x179.png 300w, https:\/\/access-diva.com\/blog\/wp-content\/uploads\/2013\/09\/OrderBy-150x89.png 150w\" sizes=\"auto, (max-width: 352px) 100vw, 352px\" \/><p id=\"caption-attachment-87\" class=\"wp-caption-text\">ORDER BY LastName<\/p><\/div>\n<p>SELECT cpFirstName, <strong>cpLastName<\/strong><br \/>\nFROM 1stTableOrQueryName<br \/>\nUNION SELECT FieldName, FieldName<br \/>\nFROM 2ndTableOrQueryName<br \/>\nUNION SELECT FieldName, FieldName<br \/>\nFROM 3ndTableOrQueryName<br \/>\n<strong>ORDER BY cpLastName<\/strong>;<\/p>\n<p>When specifying the <strong>ORDER BY<\/strong>\u00a0you must use the FieldName from the first query as in the subsequent Tables and\/or Queries the Field Names may not match or exist.<\/p>\n<p><strong>7.<\/strong> You can use <strong>Placeholders<\/strong> to *add* additional information (columns) to your query as shown in (5.), i.e&#8230;<\/p>\n<p>For numeric, because the Field will default to TEXT you must declare it as NUMERIC&#8230;<\/p>\n<p>SELECT FieldName, FieldName, <strong>Val(1) AS TypeID<\/strong><br \/>\nFROM 1stTableOrQueryName<br \/>\nUNION SELECT FieldName, FieldName, Val(2)<br \/>\nFROM 2ndTableOrQueryName<br \/>\nUNION SELECT FieldName, FieldName, Val(3)<br \/>\nFROM 3ndTableOrQueryName;<\/p>\n<p>&#8230;or for TEXT just type as usual&#8230;<\/p>\n<p>SELECT FieldName, FieldName, <strong>&#8220;M&#8221; AS ItemID<\/strong><br \/>\nFROM 1stTableOrQueryName<br \/>\nUNION SELECT FieldName, FieldName, <strong>&#8220;C&#8221;<\/strong><br \/>\nFROM 2ndTableOrQueryName<br \/>\nUNION SELECT FieldName, FieldName, <strong>&#8220;S&#8221;<\/strong><br \/>\nFROM 3ndTableOrQueryName;<\/p>\n<p>Now, when you run your SELECT query you can filter on the <strong>Placeholder<\/strong>.<\/p>\n<p><strong>8.<\/strong> You can use <strong>IIf()<\/strong> <strong>Statements<\/strong> in your UNION query, however, because this type of query can run slowly, use sparingly&#8230;<\/p>\n<p>SELECT FieldName, FieldName, Val(1)<br \/>\nFROM 1stTableOrQueryName<br \/>\nUNION SELECT FieldName, FieldName, Val(2)<br \/>\nFROM 2ndTableOrQueryName<br \/>\nUNION SELECT FieldName, FieldName, <strong>IIf([cpChecked]=0,Val(3)<\/strong><br \/>\nFROM 3ndTableOrQueryName;<\/p>\n<p>You&#8217;ll notice the Field *<strong>cpChecked<\/strong>* is not in the query itself.\u00a0 It does not have to be, it only needs to be in the Table or Query used.<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_80\" class=\"pvc_stats all  \" data-element-id=\"80\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/access-diva.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A UNION query takes data from two (or more) tables and\/or queries and makes it appear as if it is from one source, i.e&#8230;<\/p>\n<p>SELECT FieldName, FieldName FROM 1stTableOrQueryName UNION SELECT FieldName, FieldName FROM 2ndTableOrQueryName UNION SELECT FieldName, FieldName FROM 3ndTableOrQueryName;<\/p>\n<p>A few points\u2026<\/p>\n<p>1. UNION queries are READ ONLY, no edits allowed!<\/p>\n<p>2. In [&#8230;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_80\" class=\"pvc_stats all  \" data-element-id=\"80\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/access-diva.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72,55],"tags":[18],"class_list":["post-80","post","type-post","status-publish","format-standard","hentry","category-queries","category-union","tag-queries","odd"],"_links":{"self":[{"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/80","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=80"}],"version-history":[{"count":12,"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/80\/revisions"}],"predecessor-version":[{"id":442,"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/80\/revisions\/442"}],"wp:attachment":[{"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/access-diva.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}