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
);
运行结果如下: