user3296793
user3296793

Reputation: 270

SQL Contains or LIKE

I am looking to run a query on a column which looks for "word 1" or "word 2" AND if either of those exist the result needs to contain "Word 3"

The column itself can be huge with massive amounts of text in it, this is what I have so far and it doesn't return what i'm looking:

SELECT * 
FROM Table1 
WHERE column LIKE "%word.1%" OR 
      column LIKE "%word.2%" AND 
      column LIKE "%word3%";

Thanks in advance

Upvotes: 0

Views: 152

Answers (2)

Raymond Nijland
Raymond Nijland

Reputation: 11602

The column itself can be huge with massive amounts of text in it

in that case i would suggest FULL TEXT indexes asLIKE '%search%' will not scale as it never can be optimized by using indexes.

And use MATCH() ... AGAINST to match something like

SELECT 
 ...
WHERE
MATCH (column )
    AGAINST ('word.1 word.2 +word3' IN BOOLEAN MODE);

MySQL uses what is sometimes referred to as implied Boolean logic, in which

+ stands for AND

- stands for NOT

[no operator] implies OR

source

Which should be the same as writting. (scaisEdge 's answer)

 SELECT 
 ...
 WHERE
    ( column LIKE "%word.1%" OR column LIKE "%word.2%" ) 
  AND
    column LIKE "%word3%"

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133390

you shoudl use () to manage the two OR coindition together

SELECT * 
FROM Table1 
WHERE ( column LIKE "%word.1%" OR column LIKE "%word.2%" ) 
  AND column LIKE "%word3%" ;

Upvotes: 3

Related Questions