I have an MySQL Query looks like that:
SELECT
client.id, client.vorname, client.nac开发者_如何学Gohname, 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?
please take a look at the operator precedence of mysql. OR
has a lower precedence than AND
, so you'll have to put this into braces to get the desired result:
client.typ = 'U'
OR
client.typ is null
the complete WHERE-block should look like this:
(
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
)
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):
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
)
)
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')
try this one instead of zahlung.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
.
精彩评论