MySQL 喜欢另一个领域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4886295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:38:28  来源:igfitidea点击:

MySQL like another field

mysqlsql-like

提问by SiberianGuy

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

我有一个包含两个字符串列的表:Url 和 ModelId。我需要返回 Url 包含 ModelId 的记录,如下所示:

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

回答by Michael Robinson

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

回答by tal952

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,'%','\%'),'_','\_'), '%'), '%')

回答by Mahee Gunturu

Here is the query:

这是查询:

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

回答by Sachin Shanbhag

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