duanhanzi8328 2016-09-09 01:02
浏览 264
已采纳

MySql排除具有特定ID的结果?

Here is my SQL statement

            SELECT * FROM schedule
            WHERE teacher_id = '$teacher_id'
            AND event_id <> '$event_id'
            AND seconds_start = '$start_time'
            OR (seconds_start < '$start_time' AND seconds_end > '$start_time')
            OR (seconds_start < '$end_time' AND seconds_end >= '$end_time')

As you can see, I want to exclude any results with a certain ID, however this seems to not be working because...

enter image description here

Shouldn't this behave as expected??

  • 写回答

3条回答 默认 最新

  • dounie0889 2016-09-09 01:09
    关注

    You obviously don't understand Operator Precedence

    You can rarely mix AND and OR operators in the same instruction, without specifying their scope with parenthesis.

    Properly indenting your code following the parenthesis also help to understand your query:

        SELECT * FROM schedule
        WHERE teacher_id = '$teacher_id'
          AND event_id <> '$event_id'
          AND ( 
                 seconds_start = '$start_time'
             OR (seconds_start < '$start_time' AND seconds_end > '$start_time')
             OR (seconds_start < '$end_time' AND seconds_end >= '$end_time')
            )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部