Reputation: 270
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
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
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
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