dongrong9053
2016-01-04 13:43
浏览 89
已采纳

MySQLi!=没有处理多个AND / OR子句

So I have two rows in my Database - user_one and user_two, these rows are occupied by User ID's as numbers for a messaging system, I have also included a support ticket system within the same message feature but am having a few issues with this code snippet...

$sqlc="SELECT * FROM ap_conversations WHERE user_one = '$user_id' AND user_two != '999' OR user_two = '$user_id' AND user_one != '999' OR user_one = '$user_id' AND user_two != '999d' OR user_two = '$user_id' AND user_one != '999d'";

As you'll notice, the support tickets ID is 999, and when it was kept to just checking that neither ID's were 999 this code worked perfectly. Although I also need it to check that neither user_one or user_two has ID's of 999 or 999d. This current code returns 1, when I know for a fact it should return 0 so I know something is defiantly going wrong and I think I may have it set up incorrectly. I have attempted to set it up like this in many different formats:

$sqlc="SELECT * FROM ap_conversations WHERE user_one = '$user_id' AND (user_two != '999' OR user_two != '999d') OR user_two = '$user_id' AND (user_one != '999' OR user_one != '999d')";

But still get the same results? Can anybody tell me what I am doing wrong here or how I can make this code perform better?

图片转代码服务由CSDN问答提供 功能建议

所以我的数据库中有两行 - user_one user_two ,这些行被用户ID占用作为消息传递系统的数字,我还在同一消息功能中包含了一个支持服务单系统,但是这个代码片段存在一些问题...... \ n

  $ sqlc =“SELECT * FROM ap_conversations WHERE user_one ='$ user_id'AND user_two!='999'ORD user_two ='$ user_id'AND user_one!='999'OR user_one ='$ user_id'  AND user_two!='999d'OR user_two ='$ user_id'AND user_one!='999d'“; 
   
 
 

正如您将注意到的那样,支持服务单ID 是 999 ,当它只是检查这两个ID都不是 999 时,这段代码完美无缺。 虽然我还需要检查 user_one user_two 是否具有 999 999d 的ID。 这个当前代码返回 1 ,当我知道它应该返回 0 ,所以我知道有些东西是出错的,我想我可能会错误地设置它。 我试图以多种不同的格式设置它:

  $ sqlc =“SELECT * FROM ap_conversations WHERE user_one ='$ user_id'AND(user_two!='999  '或user_two!='999d')OR user_two ='$ user_id'AND(user_one!='999'ORer_one!='999d')“; 
   
 
 

但仍然得到相同的结果? 任何人都可以告诉我这里我做错了什么,或者我如何让这段代码表现得更好?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • douyan1970 2016-01-04 14:05
    最佳回答
       (user_one = '$user_id' AND user_two != '999')
    OR (user_one = '$user_id' AND user_two != '999d') 
    

    ...is a problem, it simplifies to user_one = '$user_id', the AND conditions become irrelevant.

    If you don't believe me, try a row where user_one = user_id and user_two = '999'.. It fails the first bracketed condition, but passes the second and is returned.

    I assume you want:

    SELECT * FROM ap_conversations 
     WHERE (user_one = :user_id AND user_two NOT IN ('999','999d')) 
        OR (user_two = :user_id AND user_one NOT IN ('999','999d'));
    

    N.B. Passing in '999' or '999d' as the $user_id will still return the support tickets. I quite like this functionality but if this is not desired you could use:

    SELECT * FROM ap_conversations 
     WHERE (user_one = :user_id OR user_two = :user_id) 
       AND user_one NOT IN ('999','999d')
       AND user_two NOT IN ('999','999d');
    
    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题