开发者

Exclude from MYSQL query when using syntax with JOIN

开发者 https://www.devze.com 2023-03-24 21:07 出处:网络
I have query SELECT table1.name, table2.wage, table3.bonus table5.shift FROM table1 INNER JOIN table2 ON table1.userid = table2.userid

I have query

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;

i need to also look in table "vacation" and exclude them from above query.

"vacation.userid" = "table1.userid"

like

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

but for some reason this still shows them in the list.


Try including the vacation table in as a LEFT JOIN and then add a WHERE clause of getting records where the vacation join failed.

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;


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;


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消