2

SQL Fiddle here: http://sqlfiddle.com/#!2/a2e41/8

I have a query which does a number of checks: whether column uidto or uidfrom contain a given value. This works fine, but as soon as I want to exclude when columns hidden1 or hidden2 contain a given value the query returns the results anyway. As soon as I take out the first uidto, uidfrom check, it returns nothing, which is expected. Is there any way to do all the checks? Is one being ignored in favor of the other?

Any ideas?

edit:

This still returns despite column hidden1 containing the value to check for:

SELECT m.threadid,
m.uidto,
m.uidfrom,
m.type,
m.hidden1,
m.hidden2
FROM messages m
WHERE m.uidto = 1
OR m.uidfrom = 1
AND m.hidden1 <> 1
AND m.hidden2 <> 1
GROUP BY threadid;

This seems to honor the exclusions, but does not do the check for uidfrom or uidto containing 1

SELECT m.threadid,
m.uidto,
m.uidfrom,
m.type,
m.hidden1,
m.hidden2
FROM messages m
WHERE m.hidden1 <> 1
AND m.hidden2 <> 1
GROUP BY threadid;

EDIT:

There are several threadid's and I pull the messages belonging to each user involved in the thread (there will only ever be two users) by checking if a value matches uidto or uidfrom, then checking if a users id is in hidden1 or hidden2.

the value of the hidden1 and hidden2 columns is the users id who has opted to hide the thread from himself.

If user 1 deletes his thread, we put his user id (1) into the hidden1 column so that he can't see it, but the other user can. if user id 22, who is involved in the thread as well wants to delete the thread, user id 22 would go in to hidden2, now neither of them can see that thread.

Message threads will only ever be seen by the person who sent the messages in it, or the person receiving them. Nobody else will be involved.

4

1 回答 1

0

试试这个查询,让我知道。

SELECT   m.threadid,
         m.uidto,
         m.uidfrom,
         m.type,
         m.hidden1,
         m.hidden2

FROM     messages m

WHERE    (m.uidto = 1 OR m.uidfrom = 1)
         AND m.hidden1 <> 1 AND m.hidden2 <> 1

GROUP    BY threadid;

括号很重要......没有它们,它假设你的意思是:

WHERE    m.uidto = 1 OR ( m.uidfrom = 1  AND m.hidden1 <> 1 AND m.hidden2 <> 1 )

而且......它是由操作员的优先顺序决定的!我打算更新我的答案以包含相关信息,但后来我发现这篇很棒的 SO Post 很好地涵盖了它: Mysql 或/和优先级?


澄清: http ://sqlfiddle.com/#!2/5f989/26/0

select  ThreadID, 

        Type, 

        UIDto, 
        Hidden1, 

        case when UIDTo is null     and Hidden1 is not null then concat('UserTo (',Hidden1,') is hidden')
             when UIDTo is not null and Hidden1 is null     then concat('UserTo (',UIDTo,') is NOT hidden')
             else  'UserTo ('+UIDto+') status is unknown' end as UIDTo_Status,

        UIDfrom, 
        Hidden2,

        case when UIDFrom is null     and Hidden2 is not null then concat('UserTo (',Hidden2,') is hidden')
             when UIDFrom is not null and Hidden2 is null     then concat('UserTo (',UIDFrom,') is NOT Hidden')
             else  'UserTo ('+UIDfrom+') status is unknown' end as UIDfrom_Status


from    messages
于 2013-11-14T00:02:37.120 回答