Home  >  Q&A  >  body text

MySQL uses another field for fuzzy query

<p>I have a table with two string columns: Url and ModelId. I need to return those records whose URL contains ModelId, similar to this query: </p> <pre class="brush:php;toolbar:false;">SELECT Id, Url, ModelId WHERE Url like "%ModelId%"</pre> <p><br /></p>
P粉333186285P粉333186285475 days ago478

reply all(2)I'll reply

  • P粉418351692

    P粉4183516922023-07-26 10:42:23

    You can't just concatenate strings, you must also escape the fields, using the special characters % and _:

    SELECT Id, Url, ModelId 
    WHERE Url LIKE CONCAT('%', REPLACE(REPLACE(ModelId,'%','\%'),'_','\_'), '%'), '%')

    reply
    0
  • P粉199248808

    P粉1992488082023-07-26 00:00:45

    SELECT Id, Url, ModelId 
    WHERE Url LIKE CONCAT('%', ModelId, '%')

    reply
    0
  • Cancelreply