{"id":7255,"date":"2014-06-03T23:25:52","date_gmt":"2014-06-03T23:25:52","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/06\/03\/mysql-join-returns-unexpected-values-collection-of-common-programming-errors\/"},"modified":"2014-06-03T23:25:52","modified_gmt":"2014-06-03T23:25:52","slug":"mysql-join-returns-unexpected-values-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/06\/03\/mysql-join-returns-unexpected-values-collection-of-common-programming-errors\/","title":{"rendered":"MySQL JOIN returns unexpected values-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to do a simple mysql request and I&#8217;m having problems.<\/p>\n<p>I have 2 tables defined like below:<\/p>\n<pre><code>currencies\n______________________________________________________________________________________\ncurrency_id | currency_name | currency_symbol | currency_active | currency_auto_update\n\nexchange_rates\n____________________________________________________________________________________________________\nexchange_rate_id | currency_id | exchange_rate_date | exchange_rate_value | exchange_rate_added_date\n<\/code><\/pre>\n<p>What I want to do is to select the last row inside <code>exchange_rates<\/code> for the active currency.<\/p>\n<p>I did it like this:<\/p>\n<pre><code>SELECT c.currency_id, c.currency_name, c.currency_symbol, c.currency_active, er.exchange_rate_id, er.exchange_rate_date, er.exchange_rate_value\nFROM currencies c\nLEFT JOIN (\n    SELECT er1.exchange_rate_id, er1.currency_id, er1.exchange_rate_date, er1.exchange_rate_value\n    FROM exchange_rates er1\n    ORDER BY er1.exchange_rate_date DESC\n    LIMIT 1\n    ) AS er\nON er.currency_id=c.currency_id\nWHERE c.currency_active='1'\n<\/code><\/pre>\n<p>This is returning me <code>NULL<\/code> values from the <code>exchange_rates<\/code> table, even if there are matching rows<\/p>\n<p>I&#8217;ve tried to remove <code>LIMIT 1<\/code> but if I do it like this is returning me all the rows for active currency, which is not the solution I want<\/p>\n<p>How should this query look like?<\/p>\n<p>Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to do a simple mysql request and I&#8217;m having problems. I have 2 tables defined like below: currencies ______________________________________________________________________________________ currency_id | currency_name | currency_symbol | currency_active | currency_auto_update exchange_rates ____________________________________________________________________________________________________ exchange_rate_id | currency_id | exchange_rate_date | exchange_rate_value | exchange_rate_added_date What I want to do is to select the last row inside exchange_rates for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7255","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7255"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7255\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}