如何检查列值是否在字符串SQL中

时间:2022-07-30 07:54:36

I have a database with 150,000 records and I need to match its FULL column value or records, with some parts of the string. **

我有一个包含15万条记录的数据库,我需要将它的全列值或记录与字符串的某些部分匹配。* *

As i want to check if the STRING contains the COLUMN records and NOT! if the COLUMN contains the string

因为我想检查字符串是否包含列记录,而不是!如果列包含字符串

** (Example below)

* *(在下面的例子)

For testing purposes Lets say the database has a TABLE , 1 COLUMN and 1 record as the records are similar to this:

出于测试目的,我们假设数据库有一个表、一个列和一个记录,因为这些记录类似如下:

  • come to me
  • 来找我

and i need to match it with this @STRING

我需要匹配这个@STRING

  • She wants to come to me now
  • 她现在想来找我

I want to execute something similar to :(but this doesn't work of course)

我想执行类似于:(但这当然不行)

SELECT * from TABLE where @STRING like '%'+COLUMN+'%'

I can't seem to solve this with SQL the usage of PHP is possible but prefer if the solution is with SQL but if the solution with PHP is available please propose it and note that the database has over 150,000 records

我似乎不能用SQL来解决这个问题PHP的使用是可能的,但是如果解决方案是用SQL的,但是如果有PHP的解决方案,请提出它并注意数据库有超过150,000条记录

2 个解决方案

#1


3  

SELECT * from TABLE where LOCATE(COLUMN, @STRING)>0;

LOCATE(a,b) returns a number giving the character location of a in b, or returns 0.

LOCATE(a,b)返回一个数字,给出a在b中的字符位置,或者返回0。

See Mysql LOCATE()

看到Mysql定位()

The docs discuss that LOCATE() is only case sensitive when one of the strings is a 'binary string'. That probably doesn't affect your use case, though if it became an issue you could CONVERT() the binary strings to a locale and use LOWER() to get lower case.

文档讨论,只有当其中一个字符串是“二进制字符串”时,LOCATE()才是区分大小写的。这可能不会影响您的用例,但是如果它成为一个问题,您可以将二进制字符串转换为语言环境并使用LOWER()来获得更低的用例。

MySQL String Functions

MySQL字符串函数

#2


2  

The dynamic like syntax for mysql is

mysql的动态类似语法是

SELECT * from TABLE where @STRING like CONCAT('%',COLUMN,'%')

#1


3  

SELECT * from TABLE where LOCATE(COLUMN, @STRING)>0;

LOCATE(a,b) returns a number giving the character location of a in b, or returns 0.

LOCATE(a,b)返回一个数字,给出a在b中的字符位置,或者返回0。

See Mysql LOCATE()

看到Mysql定位()

The docs discuss that LOCATE() is only case sensitive when one of the strings is a 'binary string'. That probably doesn't affect your use case, though if it became an issue you could CONVERT() the binary strings to a locale and use LOWER() to get lower case.

文档讨论,只有当其中一个字符串是“二进制字符串”时,LOCATE()才是区分大小写的。这可能不会影响您的用例,但是如果它成为一个问题,您可以将二进制字符串转换为语言环境并使用LOWER()来获得更低的用例。

MySQL String Functions

MySQL字符串函数

#2


2  

The dynamic like syntax for mysql is

mysql的动态类似语法是

SELECT * from TABLE where @STRING like CONCAT('%',COLUMN,'%')