{"id":2062,"date":"2014-12-09T10:41:07","date_gmt":"2014-12-09T18:41:07","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2062"},"modified":"2015-04-07T08:46:42","modified_gmt":"2015-04-07T15:46:42","slug":"inner-joins","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2062","title":{"rendered":"Inner Joins"},"content":{"rendered":"<p>I\u2019m re-reading the Visual QuickStart Guide to SQL and working out some examples. I don\u2019t use a whole lot of joins on my websites and I\u2019ve never given the topic much thought. In fact, if you asked me, I would have said that I never use joins, because none of my code uses the word.<\/p>\n<p>It turns out that a lot of the queries on my websites use INNER JOINs, but without using the INNER JOIN keyword. In this example I have a table of products and a table of categories. Each product belongs to one category and the category key is in the product table. In this case the category table has unique values for for `name`.<\/p>\n<p>I frequently use something like this:<br \/>\n<code><br \/>\nSELECT product.name, product_category.name<br \/>\nFROM product, product_category<br \/>\nWHERE product.category_id = product_category.id<br \/>\n<\/code><br \/>\nWhich is equivalent to this using JOIN syntax.<br \/>\n<code><br \/>\nSELECT product.name, category.name<br \/>\nFROM product<br \/>\nINNER JOIN product_category AS category<br \/>\nON product.category_id = category.id<br \/>\n<\/code><\/p>\n<p>Here\u2019s an example where I combine three tables. The product table has information about the product. It has a unique id for each product. The details table has selling information about the product. This table has a one-to-one mapping to the product table using `product_id`. The category table is the same table as in the example above. It is a one-to-many mapping. Note that the order in which the joins are made doesn\u2019t matter.<\/p>\n<p><code><br \/>\nSELECT product.name, details.itemNum, details.price, category.name, details.taxable, details.shipping_charge<br \/>\nFROM product, product_details AS details, product_category AS category<br \/>\nWHERE product.id = details.product_id<br \/>\nAND product.category_id = category.id<br \/>\nAND  details.isForSale = 'true'<br \/>\nORDER BY product.name<br \/>\n<\/code><\/p>\n<p>Which is equivalent to this using JOIN syntax.<br \/>\n<code><br \/>\nSELECT product.name, details.itemNum, details.price, category.name, details.taxable, details.shipping_charge<br \/>\nFROM product<br \/>\nINNER JOIN product_details AS details<br \/>\nON product.id = product_id<br \/>\nINNER JOIN product_category AS category<br \/>\nON product.category_id = category.id<br \/>\nWHERE  details.isForSale = 'true'<br \/>\nORDER BY product.name<br \/>\n<code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019m re-reading the Visual QuickStart Guide to SQL and working out some examples. I don\u2019t use a whole lot of joins on my websites and I\u2019ve never given the topic much thought. In fact, if you asked me, I would have said that I never use joins, because none of my code uses the word. &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2062\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Inner Joins<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-2062","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2062","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2062"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2062\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}