条件的S​​QL查询问题

时间:2013-07-17 14:20:58

标签: sql

我在这里有这个问题:

SELECT formam.*, SMS_MONTIME1.IDTICKET, SMS_MONTIME1.MBYLLUR,SMS_MONTIME1.time_added
FROM formam 
LEFT JOIN SMS_MONTIME1 ON formam.ID = SMS_MONTIME1.IDTICKET WHERE SMS_MONTIME1.IDTICKET =''
    or SMS_MONTIME1.IDTICKET is null
    AND dyqani_pergjegjes=$dyqanip
ORDER BY formam.data_fillim DESC

问题是,它工作正常,但当我添加条件AND dyqani_pergjegjes=$dyqanip它不再有效时,我没有得到任何错误,但我只是没有得到所需的结果。

有人请帮忙

1 个答案:

答案 0 :(得分:1)

这将解决问题:

SELECT formam.*, SMS_MONTIME1.IDTICKET, SMS_MONTIME1.MBYLLUR,SMS_MONTIME1.time_added
FROM formam 
LEFT JOIN SMS_MONTIME1 ON formam.ID = SMS_MONTIME1.IDTICKET WHERE SMS_MONTIME1.IDTICKET =''
    OR (SMS_MONTIME1.IDTICKET is null
    AND dyqani_pergjegjes=$dyqanip)
ORDER BY formam.data_fillim DESC

我改变的是我把括号括在

  

SMS_MONTIME1.IDTICKET为空并且dyqani_pergjegjes = $ dyqanip

确保OR和AND的正确优先级。