简体   繁体   中英

how to fix sql query?

In this noti_card_data my DB schema

  id      | date  |        cmd  |     mbrNo
--------------------------------------------
  1        2020-04-01      success    1
  2        2020-04-29      fail       -

I Want result

  id      | date  |        cmd  |     mbrNo
--------------------------------------------
  1        2020-04-01      success    1

But, sending a query like this results.

SELECT * FROM noti_card_data WHERE mbrNo = '1'  OR mbrNo = '-'  AND cmd ='success' 



 id      | date  |        cmd  |     mbrNo
--------------------------------------------
  1        2020-04-01      success    1
  2        2020-04-29      fail       -

How to fix my query?

Try the following, here is the demo .

SELECT * 
FROM noti_card_data 
WHERE (mbrNo = '1'
OR mbrNo = '-')
AND cmd ='success'

Output:

| id  | date       | cmd     | mbrNo |
| --- | ---------- | ------- | ----- |
| 1   | 2020-04-01 | success | 1     |

Instead of

mbrNo = '1'  OR mbrNo = '-'

you can say

mbrNo IN ('1', '-')

Although those two expressions are identical, the change would have coincidentally fixed your problem.

The real "problem" is

a OR b AND c   means   a OR (b AND c)

but you wanted

                       (a OR b) AND c

See "precedence of operators".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM