2022-12-01:从不订购的客户。找出所有从不订购任何东西的客户,以下数据的答案输出是Henry和Max

2022-12-01:从不订购的客户。找出所有从不订购任何东西的客户,以下数据的答案输出是Henry和Max,sql语句如何写?

DROP TABLE IF EXISTS `customers`;
CREATE TABLE `customers` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `customers` VALUES ('1', 'Joe');
INSERT INTO `customers` VALUES ('2', 'Henry');
INSERT INTO `customers` VALUES ('3', 'Sam');
INSERT INTO `customers` VALUES ('4', 'Max');

DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
  `id` int(11) NOT NULL,
  `customer_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `orders` VALUES ('1', '3');
INSERT INTO `orders` VALUES ('2', '1');

答案2022-12-01:

sql语句如下:




























select customers.name 
from customers
where customers.id not in
(
    select customer_id from orders
);

运行结果如下:
在这里插入图片描述

本作品采用《CC 协议》,转载必须注明作者和本文链接
微信公众号:福大大架构师每日一题。最新面试题,涉及golang,rust,mysql,redis,云原生,算法,分布式,网络,操作系统。
讨论数量: 1

sql SELECT customers.name FROM customers LEFT JOIN orders on customers.id = orders.customer_id WHERE orders.id is null

1个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!