How Do You or Two Like Statements?

I have tried the following two statements:

  • SELECT col FROM db.tbl WHERE col (LIKE 'str1' OR LIKE 'str2') AND col2 = num results in a syntax error
  • SELECT col FROM db.tbl WHERE page LIKE ('str1' OR 'str2') AND col2 = num results in "Truncated incorrect DOUBLE value: str1" and "Truncated incorrect DOUBLE value: str2" for what looks like every result. However, no results are actually returned.

I figured one of the two statements would work, but they aren't.

8 Answers

SELECT col FROM db.tbl WHERE (col LIKE 'str1' OR col LIKE 'str2') AND col2 = num

I believe you need WHERE ((page LIKE 'str1') OR (page LIKE 'str2'))

0

USE % at the Start and End of the String. So that it will check the Sub-Strings as well

SELECT col FROM db.tbl WHERE (col LIKE '%str1%' OR col LIKE '%str2%') AND col2 = num
0

Think simple:

SELECT col FROM db.tbl WHERE (col LIKE 'str1' OR col LIKE 'str2') AND col2 = ...

The "OR" operator applies to expressions. "LIKE 'str1'" and "LIKE 'str2'" are not valid expressions, since the "LIKE" operator requires both a left hand side and a right hand side.

page LIKE ('str1' OR 'str2')

would first try to OR together 'str1' and 'str2', giving the value TRUE. It would then try and evaluate page LIKE TRUE, and report an error.

1

try SELECT col FROM db.tbl WHERE (col LIKE 'str1' or col LIKE 'str2') AND col2 = num

I assume what you want is,

SELECT col FROM db.tbl WHERE col LIKE '%str[12]%' AND col2 = num

That will literally find all cases where (col like %str1% or col like %str2%) AND col2 = num

1

In recent versions of SQL you can use the IN operator:

SELECT col FROM db.tbl WHERE col IN('str1','str2') AND col2 = num

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest