SQL LEFT JOIN与IN使用案例说明
Posted ShenLiang2025
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL LEFT JOIN与IN使用案例说明相关的知识,希望对你有一定的参考价值。
问题描述
为什么第一个query不正确?
求sales person:
query 1:
select SalesPerson.name
from Orders
inner join Company on Company.com_id = Orders.com_id
inner join SalesPerson on SalesPerson.sales_id = Orders.sales_id
where Company.name<>'RED';
query 2:
select name
from salesperson
where sales_id not in (select orders.sales_id
from orders inner join company on company.com_id=orders.com_id
where company.name='RED')
注:原需求是输出salesperson表中没有向公司RED 销售任何东西的销售员
数据补充
--- 有如下表、结构及数据,计用SQL编写出
-- 输出salesperson表中没有向公司RED 销售任何东西的销售员
DROP TABLE IF EXISTS `company`;
CREATE TABLE `company` (
`com_id` int(11) NULL DEFAULT NULL,
`NAME` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`city` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb4;
INSERT INTO `company` VALUES (1, 'RED', 'Boston');
INSERT INTO `company` VALUES (2, 'ORANGE', 'New York');
INSERT INTO `company` VALUES (3, 'YELLOW', 'Boston');
INSERT INTO `company` VALUES (4, 'GREEN', 'Austin');
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`order_id` int(11) NULL DEFAULT NULL,
`order_date` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`com_id` int(11) NULL DEFAULT NULL,
`sales_id` int(11) NULL DEFAULT NULL,
`amount` int(11) NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb4;
INSERT INTO `orders` VALUES (1, '1/1/2014', 3, 4, 100000);
INSERT INTO `orders` VALUES (2, '2/1/2014', 4, 5, 5000);
INSERT INTO `orders` VALUES (3, '3/1/2014', 1, 1, 50000);
INSERT INTO `orders` VALUES (4, '4/1/2014', 1, 4, 25000);
CREATE TABLE `salesperson` (
`sales_id` int(11) NULL DEFAULT NULL,
`NAME` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`salary` int(11) NULL DEFAULT NULL,
`commission_rate` int(11) NULL DEFAULT NULL,
`hire_date` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb4;
INSERT INTO `salesperson` VALUES (1, ' John ', 100000, 6, ' 4/1/2006');
INSERT INTO `salesperson` VALUES (2, ' Amy ', 120000, 5, ' 5/1/2010 ');
INSERT INTO `salesperson` VALUES (3, ' Mark ', 65000, 12, ' 12/25/2008');
INSERT INTO `salesperson` VALUES (4, ' Pam ', 25000, 25, ' 1/1/2005 ');
INSERT INTO `salesperson` VALUES (5, ' Alex ', 50000, 10, ' 2/3/2007 ');
解决方法
#2 解决方法:
-- way1用LEFT JOIN结合IFNULL。
-- 解释1 这里要取得销售人员名在salesperson,所以它是主表。
-- 解释2 通过LEFT JOIN WHERE IS NULL达到NOT IN的效果。
SELECT A.name,D.sales_id
FROM salesperson as A
LEFT JOIN
(
SELECT C.sales_id
FROM orders as C
JOIN company as B
ON B.com_id = C.com_id
WHERE B.name= "RED"
) D
ON A.sales_id = D.sales_id
WHERE D.sales_id IS NULL
-- way2 用NOT IN
select name
from salesperson
where sales_id not in (select orders.sales_id
from orders inner join company on company.com_id=orders.com_id
where company.name='RED')
问题分析
-- Query1的解释:
/*
Query1 用的是INNER JOIN结果不对
1 首先记录上不对,LEFT JOIN和INNER JOIN的结果不同
2 像 Pam既销售给了“RED”又销售给了“YELLOW”但INNER JOIN的where company.name<>'RED'又给“找回来”了是不对的。
*/
以上是关于SQL LEFT JOIN与IN使用案例说明的主要内容,如果未能解决你的问题,请参考以下文章
sql中in和exist语句的区别?(补充了left join和right join)
sql中 INNER JOIN LEFT JOIN RIGHT JOIN FULL JOIN 中 ON与Where的区别
sql中 INNER JOIN LEFT JOIN RIGHT JOIN FULL JOIN 中 ON与Where的区别