{"id":88,"date":"2006-10-05T07:50:05","date_gmt":"2006-10-05T12:50:05","guid":{"rendered":"\/cgi\/wp\/?p=88"},"modified":"2006-10-05T07:50:05","modified_gmt":"2006-10-05T12:50:05","slug":"complicated-sql-queries","status":"publish","type":"post","link":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/?p=88","title":{"rendered":"Complicated SQL Queries"},"content":{"rendered":"<p>Yesterday I spent the most time working on some complicated SQL queries. I learned how to use table aliases to reference the same table twice. My query was returning multiple records for a customer because some customers had multiple addresses. I wanted just the last address based on the maximum Address record ID number. <\/p>\n<p>So I needed to select the Address ID from one table alias that matched the Maximum Address ID from the second table alias.  The query is shown below:<\/p>\n<p><code>SELECT Customers.uid, Customers.FirstName, Customers.LastName, Addr.Address1, Addr.Address2, Addr.City, Addr.State, Addr.Zip, Addr.Country, Addr.Phone, Customers.Email, CustomerPriceGroups.GroupName, Customers.Member_Expiration, MAX(Orders.DateOrdered) AS DateOrdered<br \/>\nFROM Customers, CustomerPriceGroups, Orders, OrderItems, Addresses Addr, Addresses Addr2<br \/>\nWHERE CustomerPriceGroups.uid = Customers.CustGroupID<br \/>\nAND Addr.CustomerID = Customers.uid<br \/>\nAND Addr2.CustomerID = Customers.uid<br \/>\nAND OrderItems.Category LIKE '%Partner%'<br \/>\nAND OrderItems.OrderID = Orders.uid<br \/>\nAND Orders.CustomerID = Customers.uid<br \/>\nGROUP BY Customers.uid, Customers.FirstName, Customers.LastName, Addr.uid, Addr.Address1, Addr.Address2, Addr.City, Addr.State, Addr.Zip, Addr.Country, Addr.Phone, Customers.Email, CustomerPriceGroups.GroupName, Customers.Member_Expiration<br \/>\nHAVING Addr.uid = MAX(Addr2.uid)<br \/>\nORDER BY Customers.uid<\/code><\/p>\n<p><b>Notes:<\/b><\/p>\n<ul>\n<li>use two table aliases to reference the same table twice<\/li>\n<li>apply any WHERE conditions to both table aliases<\/li>\n<li>select fields from one table alias<\/li>\n<li>include the ID field in the GROUP BY clause<\/li>\n<li>HAVING is very similar to WHERE except the statements within it are of an aggregate nature.<\/li>\n<li>ORDER BY must be after HAVING clause<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday I spent the most time working on some complicated SQL queries. I learned how to use table aliases to reference the same table twice. My query was returning multiple records for a customer because some customers had multiple addresses. &hellip; <a href=\"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/?p=88\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts\/88"}],"collection":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=88"}],"version-history":[{"count":0,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts\/88\/revisions"}],"wp:attachment":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}