简体   繁体   English

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

[英]How to check if a column value is in a string SQL

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. 我有一个包含150,000条记录的数据库,我需要将其FULL列值或记录与字符串的某些部分进行匹配。 ** **

As i want to check if the STRING contains the COLUMN records and NOT! 因为我想检查STRING是否包含COLUMN记录而不是! if the COLUMN contains the string 如果COLUMN包含字符串

** (Example below) ** (以下示例)

For testing purposes Lets say the database has a TABLE , 1 COLUMN and 1 record as the records are similar to this: 出于测试目的,假设数据库具有TABLE ,1 COLUMN和1条记录,因为记录与此类似:

  • 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条记录

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 LOCATE()

The docs discuss that LOCATE() is only case sensitive when one of the strings is a 'binary string'. 文档讨论了LOCATE()仅在字符串之一是“二进制字符串”时才区分大小写。 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. 这可能不会影响您的用例,但是如果出现问题,您可以将二进制字符串转换为语言环境,并使用LOWER()获得小写字母。

MySQL String Functions MySQL字符串函数

mysql的动态类似语法是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM