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');``````

sql语句如下：

``````

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

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

1个月前 评论

209

10

19

5