-1

my target where qq blank AND match ab with OR condition.

SELECT
    xyz
FROM
    admin 
WHERE
    qq = ''
    AND ab LIKE 'some1' 
    OR ab LIKE 'some2' 
    OR ab LIKE 'some3'
Jannat Pia
  • 31
  • 7
  • You have a table called 'col'? – Strawberry Jun 15 '19 at 11:58
  • The requirement is a bit unclear, at least to me. Could you share some sample data and the result you want to get for it? – Mureinik Jun 15 '19 at 11:59
  • sorry, just edited – Jannat Pia Jun 15 '19 at 12:08
  • What has stopped you from reading about precedence of OR & AND in SQL? (Obviously--) This is a faq. Before considering posting please always google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names; read many answers. If you post a question, use one phrasing as title. See [ask] & the voting arrow mouseover texts. – philipxy Jun 15 '19 at 12:46
  • Possible duplicate of [SQL Logic Operator Precedence: And and Or](https://stackoverflow.com/questions/1241142/sql-logic-operator-precedence-and-and-or) – philipxy Jun 15 '19 at 12:49

4 Answers4

3

Depending on your collation, this particular query can be written like this:

SELECT xyz
 FROM col_admin 
WHERE qq = ''
  AND ab IN('some1','some2','some3')

More generally, the astute use of parentheses will suffice...

SELECT xyz
  FROM col_admin 
 WHERE qq = ''
   AND ( ab LIKE 'some1' 
      OR ab LIKE 'some2' 
      OR ab LIKE 'some3'
       )
Strawberry
  • 33,750
  • 13
  • 40
  • 57
1

The script syntax will be as follow. This will ensure the QQ Is blank and ab match any of the text.

SELECT xyz
FROM col_admin 
WHERE qq = ''
AND (ab LIKE '%some1%' OR ab LIKE '%some2%'  OR ab LIKE '%some3%')
mkRabbani
  • 16,295
  • 2
  • 15
  • 24
  • yeh, sure, i will when stackoverflow make me as a moderator :P and the moment my permission to view only. hehe – Jannat Pia Jun 15 '19 at 12:13
1

Use in:

WHERE qq = '' AND ab IN ('some1', 'some2', 'some3')

You are not using wildcards, so there is no need to use LIKE.

You don't seem to understand how AND and OR work together. Because you don't understand precedence (yet), use parentheses whenever your conditions have both AND and OR.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

You can accomplish this by putting braces in the condition

SELECT
    xyz
FROM
    col_admin 
WHERE
    qq = ''
    AND (ab LIKE 'some1' 
    OR ab LIKE 'some2' 
    OR ab LIKE 'some3')