{"id":1196,"date":"2013-05-04T11:36:06","date_gmt":"2013-05-04T01:36:06","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=1196"},"modified":"2013-05-04T11:36:06","modified_gmt":"2013-05-04T01:36:06","slug":"mysql-join-versus-subquery-and-having-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/mysql-join-versus-subquery-and-having-primer-tutorial\/","title":{"rendered":"MySql Join versus Subquery and Having Primer Tutorial"},"content":{"rendered":"<p>Below we have some MySql (but it is very generic in its ideas &#8230; particularly if you get rid of the `s), using phpMyAdmin, showing a table Join versus the same functionality using a Subquery approach, then each of these with a Having clause, using a Drupal database.<\/p>\n<blockquote><p>SELECT u.`name` , n.`type` , count( * )<br \/>\n  FROM `users` u, `node` n<br \/>\n  WHERE u.`uid` = n.`uid`<br \/>\n  GROUP BY u.`uid` , n.`type`<br \/>\n  ORDER BY n.`uid`, n.`type`;<\/p><\/blockquote>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/MySql\/SubqueryJoinHaving\/SQL_Subquery_Join-01of.jpg\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"MySql Join versus Subquery and Having Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/MySql\/SubqueryJoinHaving\/SQL_Subquery_Join-01of.jpg\" title=\"MySql Join versus Subquery and Having Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">MySql Join versus Subquery and Having Primer Tutorial<\/p><\/div>\n<blockquote><p>SELECT (<br \/>\n  SELECT u.`name`<br \/>\n    FROM `users` u<br \/>\n    WHERE u.`uid` = n.`uid`<br \/>\n    ) AS OurName, n.`type` , count( * )<br \/>\n  FROM `node` n<br \/>\n  GROUP BY (<br \/>\n    SELECT u.`name`<br \/>\n      FROM `users` u<br \/>\n      WHERE u.`uid` = n.`uid`<br \/>\n      ), n.`type`<br \/>\n  ORDER BY n.`uid`, n.`type`;<\/p><\/blockquote>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/MySql\/SubqueryJoinHaving\/SQL_Subquery_Join-02of.jpg\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"MySql Join versus Subquery and Having Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/MySql\/SubqueryJoinHaving\/SQL_Subquery_Join-02of.jpg\" title=\"MySql Join versus Subquery and Having Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">MySql Join versus Subquery and Having Primer Tutorial<\/p><\/div>\n<blockquote><p>SELECT u.`name` , n.`type` , count( * )<br \/>\n  FROM `users` u, `node` n<br \/>\n  WHERE u.`uid` = n.`uid`<br \/>\n  GROUP BY u.`uid` , n.`type`<br \/>\n  HAVING count( * ) > 1<br \/>\n  ORDER BY n.`uid`, n.`type`;<\/p><\/blockquote>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/MySql\/SubqueryJoinHaving\/SQL_Subquery_Join-01Havingof.jpg\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"MySql Join versus Subquery and Having Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/MySql\/SubqueryJoinHaving\/SQL_Subquery_Join-01Havingof.jpg\" title=\"MySql Join versus Subquery and Having Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">MySql Join versus Subquery and Having Primer Tutorial<\/p><\/div>\n<blockquote><p>SELECT (<br \/>\n  SELECT u.`name`<br \/>\n    FROM `users` u<br \/>\n    WHERE u.`uid` = n.`uid`<br \/>\n    ) AS OurName, n.`type` , count( * )<br \/>\n  FROM `node` n<br \/>\n  GROUP BY (<br \/>\n    SELECT u.`name`<br \/>\n      FROM `users` u<br \/>\n      WHERE u.`uid` = n.`uid`<br \/>\n      ), n.`type`<br \/>\n  HAVING count( * ) > 1<br \/>\n  ORDER BY n.`uid`, n.`type`;<\/p><\/blockquote>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/MySql\/SubqueryJoinHaving\/SQL_Subquery_Join-02Havingof.jpg\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"MySql Join versus Subquery and Having Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/MySql\/SubqueryJoinHaving\/SQL_Subquery_Join-02Havingof.jpg\" title=\"MySql Join versus Subquery and Having Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">MySql Join versus Subquery and Having Primer Tutorial<\/p><\/div>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d1196' onclick='var dv=document.getElementById(\"d1196\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?cat=47\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d1196' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Below we have some MySql (but it is very generic in its ideas &#8230; particularly if you get rid of the `s), using phpMyAdmin, showing a table Join versus the same functionality using a Subquery approach, then each of these &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/mysql-join-versus-subquery-and-having-primer-tutorial\/\">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":[9,12,37],"tags":[299,827,1186,1319],"class_list":["post-1196","post","type-post","status-publish","format-standard","hentry","category-database","category-elearning","category-tutorials","tag-database-2","tag-mysql","tag-sql","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/1196"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=1196"}],"version-history":[{"count":0,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/1196\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=1196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=1196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=1196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}