{"id":493,"date":"2012-09-07T07:40:38","date_gmt":"2012-09-07T07:40:38","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=493"},"modified":"2024-10-28T08:01:20","modified_gmt":"2024-10-28T08:01:20","slug":"join-to-get-results-from-2-tables","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/mysql\/queries\/select\/join-to-get-results-from-2-tables","title":{"rendered":"JOIN to get results from 2 tables"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/ibex.tech\/cloud\/wp-content\/uploads\/sites\/9\/sql_join_types.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"737\" height=\"1024\" src=\"https:\/\/ibex.tech\/cloud\/wp-content\/uploads\/sites\/9\/sql_join_types-737x1024.jpg\" alt=\"\" class=\"wp-image-4703\" srcset=\"https:\/\/ibex.tech\/cloud\/wp-content\/uploads\/sites\/9\/sql_join_types-737x1024.jpg 737w, https:\/\/ibex.tech\/cloud\/wp-content\/uploads\/sites\/9\/sql_join_types-216x300.jpg 216w, https:\/\/ibex.tech\/cloud\/wp-content\/uploads\/sites\/9\/sql_join_types-768x1067.jpg 768w, https:\/\/ibex.tech\/cloud\/wp-content\/uploads\/sites\/9\/sql_join_types-1106x1536.jpg 1106w, https:\/\/ibex.tech\/cloud\/wp-content\/uploads\/sites\/9\/sql_join_types-43x60.jpg 43w, https:\/\/ibex.tech\/cloud\/wp-content\/uploads\/sites\/9\/sql_join_types-108x150.jpg 108w, https:\/\/ibex.tech\/cloud\/wp-content\/uploads\/sites\/9\/sql_join_types.jpg 1125w\" sizes=\"auto, (max-width: 737px) 100vw, 737px\" \/><\/a><\/figure>\n<\/div>\n\n\n<h4 class=\"wp-block-heading\">Using JOIN to get results from two tables<\/h4>\n\n\n\n<p><span style=\"color: #ff0000;\"><em><strong>This is PHP4 Code!<\/strong><\/em><\/span><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Example 1<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\t$result = @mysql_fetch_array(@mysql_query(\"\n\t\tSELECT *\n\t\tFROM group_invitations\n\t\tJOIN member_profile\n\t\tON member_profile.user_id = group_invitations.user_id\n\t\tWHERE indexer = $indexer\n\t\t\"));<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Example 2<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$query = @mysql_query(\"\n  SELECT * FROM group_profile AS GP\n  JOIN group_membership AS GM ON GM.group_id = GP.indexer\n  WHERE (member_id = $user_id AND GP.indexer = $group_id) AND (GM.approved = 'yes' OR GP.public_private = 'public');\n\");\nif (@mysql_num_rows($query) == 0)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Using JOIN to get results from three tables<\/h4>\n\n\n\n<p><span style=\"color: #ff0000;\"><em><strong>This is PHP4 Code!<\/strong><\/em><\/span><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Example 1<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$query102 = @mysql_query(\"\n\tSELECT\tPP.indexer AS project_id,\n\t\t\t\tproject_name,\n\t\t\t\tproject_picture,\n\t\t\t\tproject_description,\n\t\t\t\tDATE_FORMAT(start_date, '%d-%m-%y') AS start_date,\n\t\t\t\tDATE_FORMAT(end_date, '%d-%m-%y') AS end_date,\n\t\t\t\tCONCAT_WS(' ', first_name, last_name) AS project_lead\n\tFROM project_profile AS PP\n\tJOIN group_membership AS GM ON GM.group_id = PP.group_id\n\tJOIN member_profile AS MP ON MP.user_id = GM.member_id\n\tWHERE PP.group_id = $group_id AND group_admin = 'yes'\n\tLIMIT $proj_set_limit, $proj_limit\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Using GROUP BY To Get One Row Per Match<\/h4>\n\n\n\n<p><span style=\"color: #ff0000;\"><em><strong>This is PHP4 Code!<\/strong><\/em><\/span><\/p>\n\n\n\n<p>Note &#8211; when using GROUP BY, if you also use ORDER BY it must come after the GROUP BY<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t$sql_query\t= \"\n\tSELECT \n\t\ttable1.indexer\n\tFROM table1\n\tJOIN table2\n\tON table2.id = table1.id\n\tWHERE\n\t\ttable1.approved = 1 AND\n\t\ttable2.type = 10\n\tGROUP BY videos_usage.usage_video_id\n\t\";\n\t\/\/Note that if using WHERE, GROUP BY must be after it\n<\/code><\/pre>\n\n\n\n<p><a href=\"http:\/\/www.w3schools.com\/sql\/sql_groupby.asp\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.w3schools.com\/sql\/sql_groupby.asp<\/a><\/p>\n\n\n\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/6896723\/mysql-join-get-one-row-per-match\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/6896723\/mysql-join-get-one-row-per-match<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using JOIN to get results from two tables This is PHP4 Code! Example 1 Example 2 Using JOIN to get results from three tables This is PHP4 Code! Example 1 Using GROUP BY To Get One Row Per Match This is PHP4 Code! Note &#8211; when using GROUP BY, if you also use ORDER BY [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-493","post","type-post","status-publish","format-standard","hentry","category-select"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=493"}],"version-history":[{"count":10,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/493\/revisions"}],"predecessor-version":[{"id":4704,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/493\/revisions\/4704"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}