{"id":1986,"date":"2018-05-28T15:55:23","date_gmt":"2018-05-28T20:55:23","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1986"},"modified":"2018-05-29T16:30:52","modified_gmt":"2018-05-29T21:30:52","slug":"mysql-run-multiple-queries-one","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/05\/28\/mysql-run-multiple-queries-one\/","title":{"rendered":"MySQL: How to run multiple queries in one"},"content":{"rendered":"<p>Let&#8217;s say we have a couple of simple SQL queries, one of which counts the total number of job hours:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">select sum(Hours) as totalCount from job_details where Job_ID = 42504;<\/pre>\n<p>and another query that sums the total number of job hours that have been completed:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">select sum(Hours) as completedCount from job_details where Job_ID = 42504 and Completed = 1;<\/pre>\n<p>If we want to combine both of these queries into a single query, we can! The trick is to use aliases for both of the selects.<\/p>\n<p>For example, here I have used the aliases &#8220;as a&#8221; and &#8220;as b&#8221;:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">select \r\n    totalCount, \r\n    completedCount \r\nfrom \r\n    (select sum(Hours) as totalCount from job_details where Job_ID = 42504) as a, \r\n    (select sum(Hours) as completedCount from job_details where Job_ID = 42504 and Completed = 1) as b;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1989\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/05\/totcompct.png\" alt=\"\" width=\"215\" height=\"70\" \/><\/p>\n<p>Here is an example of wrapping 5 SQL queries into one:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">select \r\n\ttotal, \r\n\tcompleted, \r\n\tdept_id, \r\n\tdeptName,\r\n\tschedStart,\r\n\tschedEnd,\r\n\tnumTasks\r\nfrom \r\n\t(select \r\n\t\tsum(details.Hours) as total \r\n\tfrom \r\n\t\tjob_details details, \r\n\t\tjob_schedule sched\r\n\twhere \r\n\t\tdetails.Job_ID = '41230' and\r\n\t\tsched.Shop_ID = '9553' and\r\n\t\tdetails.Job_ID = sched.Job_ID) as a,\r\n\r\n\t(select \r\n\t\tsum(details.Hours) as completed \r\n\tfrom \r\n\t\tjob_details details, \r\n\t\tjob_schedule sched\r\n\twhere \r\n\t\tdetails.Job_ID = '41230' and \r\n\t\tdetails.Completed = 1 and\r\n\t\tsched.Shop_ID = '9553' and\r\n\t\tdetails.Job_ID = sched.Job_ID) as b,\r\n\r\n\t(select \r\n\t\tstore.Dept_ID as dept_id, \r\n\t\twebsite.Department as deptName \r\n\tfrom \r\n\t\tstore.job_details store, \r\n\t\tjob_schedule sched,\r\n\t\twebsite.customer_service_department website \r\n\twhere \r\n\t\tstore.Job_ID = '41230' and \r\n\t\tsched.Shop_ID = '9553' and\r\n\t\tstore.Completed = 0 and \r\n\t\tstore.Dept_ID = website.ID and\r\n\t\tstore.Job_ID = sched.Job_ID) as c,\r\n\r\n\t(select \r\n\t\tScheduled_Start as schedStart,  \r\n\t\tScheduled_End as schedEnd\r\n\t\tfrom job_schedule sched\r\n\twhere \r\n\t\tJob_ID = '41230' and \r\n\t\tShop_ID = '9553') as d,\r\n\r\n\t(select \r\n\t\tcount(Description) as numTasks \r\n\tfrom \r\n\t\tjob_details \r\n\twhere \r\n\t\tJob_ID = '41230') as e;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1991\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/05\/five-queries.png\" alt=\"\" width=\"797\" height=\"75\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/05\/five-queries.png 797w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/05\/five-queries-300x28.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/05\/five-queries-768x72.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/05\/five-queries-676x64.png 676w\" sizes=\"auto, (max-width: 797px) 100vw, 797px\" \/><\/p>\n<p>Notes:<\/p>\n<ul>\n<li>Each of the columns in the output are listed in the inital select at the top of the overall query. For example, the first column &#8220;total&#8221; is returned by the first query &#8220;a&#8221;.<\/li>\n<li>In the FROM portion of the overall query, there are 5 sub-queries, with aliases a &#8211; e.<\/li>\n<li>Each sub-query starts with select and is wrapped in parenthesis.<\/li>\n<li>The output of each sub-query feeds the top level selects.<\/li>\n<li>It is possible for a sub-query to return more than one output. For example sub-query c returns both dept_id and deptName.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say we have a couple of simple SQL queries, one of which counts the total number of job hours: select sum(Hours) as totalCount from job_details where Job_ID = 42504; and another query that sums the total number of job hours that have been completed: select sum(Hours) as completedCount from job_details where Job_ID = 42504 &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/05\/28\/mysql-run-multiple-queries-one\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">MySQL: How to run multiple queries in one<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[108,43],"class_list":["post-1986","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-mysql","tag-sql"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1986"}],"version-history":[{"count":8,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1986\/revisions"}],"predecessor-version":[{"id":2015,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1986\/revisions\/2015"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}