开发者

Something wrong with my mySQL query

SELECT t.id, t.active, t.off, t.duration, t.user
FROM table t
WHERE t.active = '0000-00-00 00:00:00'
OR t.duration != '0'
AND开发者_Go百科 t.user = '1'
AND t.off = '0000-00-00 00:00:00'
LIMIT 0 , 30

Basically it's ignoring the t.off part, whats wrong?

Thank you =)

(the query was edited for an example purpose, the real query is similar to this but with other table names, etc)

== [ Update ] ==

SELECT t.active, t.off, t.duration, t.user
FROM table t
WHERE (
    t.active = '0000-00-00 00:00:00'
    OR t.duration != '0'
)
AND t.user = '1'
AND t.off = '0000-00-00 00:00:00'
LIMIT 0 , 30
UNION ALL
SELECT c.active, c.off, c.duration, c.user
FROM chair c
WHERE (
    c.active = '0000-00-00 00:00:00'
    OR c.duration != '0'
)
AND c.user = '1'
AND c.off = '0000-00-00 00:00:00'


You should use parenthesis, to group conditions (and / or), to make sure they are evaluated in the order you want.

For example, do you want to following :

SELECT t.id, t.active, t.off, t.duration, t.user
FROM table t
WHERE t.active = '0000-00-00 00:00:00'
OR (
    t.duration != '0'
    AND t.user = '1'
    AND t.off = '0000-00-00 00:00:00'
)
LIMIT 0 , 30

Or the following :

SELECT t.id, t.active, t.off, t.duration, t.user
FROM table t
WHERE (
    t.active = '0000-00-00 00:00:00'
    OR t.duration != '0'
)
AND t.user = '1'
AND t.off = '0000-00-00 00:00:00'
LIMIT 0 , 30

Or maybe something else ?


Using parenthesis will make your query easier to read -- and will help making sure the conditions are evaluated properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜