{"id":23,"date":"2012-10-01T06:36:58","date_gmt":"2012-10-01T04:36:58","guid":{"rendered":"http:\/\/icefront.info\/wp\/?p=23"},"modified":"2012-10-01T06:36:58","modified_gmt":"2012-10-01T04:36:58","slug":"mysql-select-field-replace-text-from-another-table","status":"publish","type":"post","link":"https:\/\/icefront.info\/index.php\/2012\/10\/01\/mysql-select-field-replace-text-from-another-table\/","title":{"rendered":"MySQL: Select field, replace text from another table"},"content":{"rendered":"<p>I have two tables, &#8220;products&#8221; and &#8220;brands&#8221;. In the &#8220;products&#8221; table, the product&#8217;s name is in the &#8220;name&#8221; field. The product&#8217;s brand is represented by the # (diez) character. To know which is the brand, there&#8217;s a &#8220;brand_id&#8221; field.<\/p>\n<p>So far we have:<\/p>\n<pre>products table:\n  id: int\n  name: varchar\n  brand_id: int\n\nbrands table:\n  id: int\n  name: varchar<\/pre>\n<p>I was thinking in the other day, can I build the name only in SQL? The problem is that not all products have a brand&#8230;<\/p>\n<p>The first approach was the <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/string-functions.html#function_replace\" target=\"_blank\">REPLACE() function<\/a>:<\/p>\n<pre>function REPLACE(str, from_str, to_str)<\/pre>\n<p>Nice, but&#8230; There&#8217;s always a but&#8230; In the documentation it doesn&#8217;t states, when <code>to_str<\/code> is NULL, the result is also NULL! Seems to me illogical, normally I expect the <code>from_str<\/code><br \/>\ncleared from str in this case&#8230;<\/p>\n<p>So, the query<\/p>\n<pre>SELECT REPLACE(name, '#', (SELECT name FROM brands WHERE id = products.brand_id)) FROM products;<\/pre>\n<p>results in several NULL rows&#8230; This is wrong, since products without brands should be displayed as-is.<\/p>\n<p>What solves the problem, is the <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/control-flow-functions.html#function_if\" target=\"_blank\">IF function<\/a>:<\/p>\n<pre>IF(condition, true_value, false_value)<\/pre>\n<p>We use the &#8220;brand_id&#8221; field as condition, we replace the diez only if <code>brand_id &gt; 0<\/code>. The final query looks like this:<\/p>\n<pre>SELECT IF(brand_id &gt; 0, (REPLACE(name, '#', (SELECT name FROM brands WHERE id = products.brand_id))), name) FROM products;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have two tables, &#8220;products&#8221; and &#8220;brands&#8221;. In the &#8220;products&#8221; table, the product&#8217;s name is in the &#8220;name&#8221; field. The product&#8217;s brand is represented by the # (diez) character. To know which is the brand, there&#8217;s a &#8220;brand_id&#8221; field. So far we have: products table: id: int name: varchar brand_id: int brands table: id: int [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":0,"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"wp:attachment":[{"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/icefront.info\/index.php\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}