MySQL like another field

你离开我真会死。 提交于 2019-11-29 01:50:23

问题


I have a table with two string columns: Url and ModelId. I need to return records for which Url contains ModelId, something like this:

SELECT Id, Url, ModelId WHERE Url like "%ModelId%"

回答1:


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



回答2:


You can not just concat the strings, you must also escape the field from % and _:

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



回答3:


Here is the query:

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



回答4:


SELECT Id, Url, ModelId from <table> WHERE Url like '%' + ModelId + '%'


来源:https://stackoverflow.com/questions/4886295/mysql-like-another-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!