OR运算符忽略AND运算符MYSQL

时间:2013-08-22 10:03:50

标签: mysql join operators

请参阅下面的查询我希望所有那些状态为使用或提供的记录,而userid不等于1且课程代码为cs3333

SELECT c.`id` AS courseid,
    c.`userid` AS userid,
    c.`coursecode`,
    m.`title`,
    s.`sem_name`,
    p.`professor`,
    st.`status`,
    a.`author_name`,
    q.`quality`,
    m.`comments`,
    m.`price`,
    m.`material`
FROM sc_courses c
    JOIN sc_c_materials m
    ON c.`id`=m.`courseid`

    JOIN sc_semesters s
    ON s.`id`=c.`semid`

    JOIN sc_professors p
    ON c.`profid`=p.`id`

    JOIN sc_status st
    ON c.`statusid`=st.`id`

    LEFT JOIN sc_authors a
    ON m.`authorid`=a.`id`

    JOIN sc_quality q
    ON m.`qualityid`=q.`id`
WHERE st.`status` = "offering" OR st.`status` = "using"  AND c.`userid` != "1" AND c.`coursecode` = "CS3333";

查询正在运行,但它忽略了和运算符上面的查询返回所有记录,其中userid为1且coursecode不等于cs3333但我不想要这些记录plz告诉我我做错了什么???? / p>

3 个答案:

答案 0 :(得分:1)

你只需要括号

WHERE (st.`status` = "offering" OR st.`status` = "using")  AND c.`userid` != "1" AND c.`coursecode` = "CS3333";

答案 1 :(得分:1)

那是因为优先权。您应该( .. )更改比较顺序。

(st.`status` = "offering" OR st.`status` = "using")  
AND c.`userid` != "1" 
AND c.`coursecode` = "CS3333"

答案 2 :(得分:0)

opss这是一个愚蠢的错误,它正在使用这个查询

SELECT c.`id` AS courseid,
    c.`userid` AS userid,
    c.`coursecode`,
    m.`title`,
    s.`sem_name`,
    p.`professor`,
    st.`status`,
    a.`author_name`,
    q.`quality`,
    m.`comments`,
    m.`price`,
    m.`material`
FROM sc_courses c
    JOIN sc_c_materials m
    ON c.`id`=m.`courseid`

    JOIN sc_semesters s
    ON s.`id`=c.`semid`

    JOIN sc_professors p
    ON c.`profid`=p.`id`

    JOIN sc_status st
    ON c.`statusid`=st.`id`

    LEFT JOIN sc_authors a
    ON m.`authorid`=a.`id`

    JOIN sc_quality q
    ON m.`qualityid`=q.`id`
WHERE c.`userid` != "1" AND c.`coursecode` = "CS3333" AND st.`status` = "offering" OR 
    c.`userid` != "1" AND c.`coursecode` = "CS3333" AND st.`status` = "using";