Undefined
Undefined

Reputation: 23

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. **

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

  • 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

Upvotes: 2

Views: 4643

Answers (2)

Paul
Paul

Reputation: 27483

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.

See Mysql LOCATE()

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.

MySQL String Functions

Upvotes: 3

FuzzyTree
FuzzyTree

Reputation: 32402

The dynamic like syntax for mysql is

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

Upvotes: 2

Related Questions