哪里不影响。为什么?
我有一个 MySQL 查询看起来像这样:
SELECT
client.id, client.vorname, client.nachname, DATE_FORMAT(geburtsDatum, '%d.%m.%Y'), zahlung.zuUebBet, zahlung.betrag
FROM
zahlung JOIN client ON ( zahlung.mitgliedsNr = client.id )
WHERE
client.typ = 'U'
OR
client.typ is null
AND
zahlung.typ = 'Beitrag'
AND
anBGueberwiesen = '0000-00-00'
AND
zahlung.zuUebBet IS NOT NULL
AND
(
zahlung.vomBGeinheb = 0
OR
zahlung.vomBGeinheb is null
)
一切正常,但情况:
zahlung.zuUebBet IS NOT NULL
看起来像是被忽略了。 (我得到的行中有很多 NULL)
有人知道发生了什么事吗?
I have an MySQL Query looks like that:
SELECT
client.id, client.vorname, client.nachname, DATE_FORMAT(geburtsDatum, '%d.%m.%Y'), zahlung.zuUebBet, zahlung.betrag
FROM
zahlung JOIN client ON ( zahlung.mitgliedsNr = client.id )
WHERE
client.typ = 'U'
OR
client.typ is null
AND
zahlung.typ = 'Beitrag'
AND
anBGueberwiesen = '0000-00-00'
AND
zahlung.zuUebBet IS NOT NULL
AND
(
zahlung.vomBGeinheb = 0
OR
zahlung.vomBGeinheb is null
)
All work fine but the case:
zahlung.zuUebBet IS NOT NULL
looks like beeing ignored. (I get rows with lots of NULL in it)
Anyone know whats going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请看一下mysql的运算符优先级。
OR
的优先级低于AND
,因此您必须将其放入大括号中才能获得所需的结果:完整的 WHERE 块应如下所示:
否则, sql 语句将被处理如下(我认为这不是你想要的)(我添加了大括号以使事情变得清晰):
please take a look at the operator precedence of mysql.
OR
has a lower precedence thanAND
, so you'll have to put this into braces to get the desired result:the complete WHERE-block should look like this:
otherwise, the sql-statement would be treated like the following (wich isn't what you wanted, i think) (i've added braces to make things clear):
在 where 条件之间使用适当的多级括号。
您在最后一部分中使用了括号,但前几个条件的 AND、OR 也需要加上括号。
use proper multi level parentheses in between your where conditions.
You have used parentheses in last part, but the AND, OR's for the first few conditions are also required to be parenthesized.
!ISNULL('zahlung.zuUebBet')
尝试这个而不是zahlung.zuUebBet IS NOT NULL
。我仍然无法解释为什么它会起作用,但它确实不止一次地改变了我的结果。对于更了解 MySQL 的人,请解释为什么
!ISNULL();
!=;不为空
。!ISNULL('zahlung.zuUebBet')
try this one instead ofzahlung.zuUebBet IS NOT NULL
. I still have no explanation why this works, but it did change results for me more than once.For anyone more knowledgable on MySQL, please explain why
!ISNULL(<row>);
!=<row> IS NOT NULL
.