<p>I'm having an issue with a query that currently uses </p>

<pre><code>LEFT JOIN weblog_data AS pwd 
  ON (pwd.field_id_41 != '' 
  AND pwd.field_id_41 LIKE CONCAT('%', ewd.field_id_32, '%'))
</code></pre>

<p>However I'm discovering that I need it to only use that if there is no exact match first. What's happening is that the query is double dipping due to the use of <code>LIKE</code>, so if it tests for an exact match first then it will avoid the double dipping issue. Can anyone provide me with any further guidance?</p>

[{'answer_id': 75861, 'author': 'Sam', 'author_id': 9406, 'author_profile': 'https://Stackoverflow.com/users/9406', 'pm_score': 1, 'selected': False, 'text': '<p>you\'re talking about short circuit evaluation.</p>\n\n<p>Take a look at this article it might help you:\n<a href="http://beingmarkcohen.com/?p=62" rel="nofollow noreferrer">http://beingmarkcohen.com/?p=62</a></p>\n'}, {'answer_id': 75884, 'author': 'Chris Ballance', 'author_id': 1551, 'author_profile': 'https://Stackoverflow.com/users/1551', 'pm_score': 1, 'selected': False, 'text': "<p>using TSQL, run an exact match, check for num of rows == 0, if so, run the like, otherwise don't run the like or add the like results below the exact matches.</p>\n"}, {'answer_id': 75899, 'author': 'Mostlyharmless', 'author_id': 12881, 'author_profile': 'https://Stackoverflow.com/users/12881', 'pm_score': 0, 'selected': False, 'text': "<p>I can only think of doing it in code. Look for an exact match, if the result is empty, look for a LIKE. \nOne other option is a WHERE within this query such that WHERE ({count from exact match}=0), in which case, it wont go through the comparison with LIKE if the exact match returns more than 0 results. But its terribly inefficient... not to mention the fact that using it meaningfully in code is rather difficult.</p>\n\n<p>i'd go for a If(count from exact match = 0) then do like query, else just use the result from exact match.</p>\n"}, {'answer_id': 75973, 'author': 'Jonathan Rupp', 'author_id': 12502, 'author_profile': 'https://Stackoverflow.com/users/12502', 'pm_score': 3, 'selected': True, 'text': "<p>It sounds like you want to join the tables aliased as pwd and ewd in your snippet based first on an exact match, and if that fails, then on the like comparison you have now.</p>\n\n<p>Try this:</p>\n\n<pre><code>LEFT JOIN weblog_data AS pwd1 ON (pwd.field_id_41 != '' AND pwd.field_id_41 = ewd.field_id_32)\nLEFT JOIN weblog_data AS pwd2 ON (pwd.field_id_41 != '' AND pwd.field_id_41 LIKE CONCAT('%', ewd.field_id_32, '%'))\n</code></pre>\n\n<p>Then, in your select clause, use something like this:</p>\n\n<pre><code>select\n  isnull(pwd1.field, pwd2.field)\n</code></pre>\n\n<p>however, if you are dealing with a field that can be null in pwd, that will cause problems, this should work though:</p>\n\n<pre><code>select\n  case pwd1.nonnullfield is null then pwd2.field else pwd1.field end\n</code></pre>\n\n<p>You'll also have to make sure to do a group by, as the join to pwd2 will still add rows to your result set, even if you end up ignoring the data in it.</p>\n"}]
['https://Stackoverflow.com/questions/75819', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12073/']