在使用JOIN语法时从MYSQL查询中排除

时间:2011-08-03 20:49:22

标签: mysql join

我有查询

SELECT table1.name, table2.wage, table3.bonus table5.shift
FROM table1
    INNER JOIN table2 ON table1.userid = table2.userid
    INNER JOIN table3 ON table2.userid = table3.userid
    LEFT JOIN table5 ON table1.userid = table5.userid;

我还需要查看表“休假”并将其从上面的查询中排除。

"vacation.userid" = "table1.userid"

喜欢

INNER JOIN vacation ON table1.userid != vacation.userid

但出于某种原因,这仍然会在列表中显示出来。

3 个答案:

答案 0 :(得分:0)

尝试将休假表作为LEFT JOIN包含在内,然后添加一个WHERE子句来获取假期加入失败的记录。

SELECT table1.name, table2.wage, table2.bonus table3.shift, table4.vacation
   FROM table1
INNER JOIN table2 ON table1.userid = table2.userid
INNER JOIN table3 ON table2.userid = table3.userid
INNER JOIN table4 ON table4.userid = table4.userid 
LEFT JOIN table5 ON table1.name = table5.position
LEFT JOIN vacation on vacation.userid = table1.userid
WHERE vacation.userid IS NULL;

答案 1 :(得分:0)

SELECT table1.name, table2.wage, table2.bonus table3.shift
FROM table1
    INNER JOIN table2 ON table1.userid = table2.userid
    INNER JOIN table3 ON table2.userid = table3.userid
    LEFT JOIN table5 ON table1.name = table5.position
    LEFT JOIN vacation ON vacation.userid = table1.userid
WHERE
    vacation.userid IS NULL;

答案 2 :(得分:0)

SELECT table1.name, table2.wage, table3.bonus table5.shift
FROM table1
    INNER JOIN table2 ON table1.userid = table2.userid
    INNER JOIN table3 ON table2.userid = table3.userid
    LEFT JOIN table5 ON table1.userid = table5.userid;
WHERE NOT EXISTS
    (SELECT * FROM vacation WHERE vacation.userid = table1.userid)