将 LIKE %..% 与 MySQL 中的字段值一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4420554/
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 17:59:06  来源:igfitidea点击:

Use LIKE %..% with field values in MySQL

mysqlsqlsql-like

提问by Max Kielland

I stumbled into a delicate SQL problem when I needed to use a value from a field inside a LIKE %..% statement.

当我需要在 LIKE %..% 语句中使用字段中的值时,我偶然发现了一个微妙的 SQL 问题。

Example:

例子:

SELECT t1.Notes, t2.Name
FROM Table1 t1, Table2 t2
WHERE t1.Notes LIKE '%t2.Name%'

This is only an example from the top of my head to show what I need to do (I know this will not work). I need to use the value of t2.Name inside the LIKE %..%

这只是我头脑中的一个例子,用来说明我需要做什么(我知道这行不通)。我需要在 LIKE %..% 中使用 t2.Name 的值

I guess this is trivial when you know it ;)

当你知道时,我想这是微不足道的;)

回答by OMG Ponies

Use:

用:

SELECT t1.Notes, 
       t2.Name
  FROM Table1 t1
  JOIN Table2 t2 ON t1.Notes LIKE CONCAT('%', t2.Name ,'%')

回答by NT4.info

  SELECT t1.a, t2.b
  FROM t1
  JOIN t2 ON t1.a LIKE '%'+t2.b +'%'

because the last answer not work

因为最后一个答案不起作用