问题描述:
Table: Product
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| product_id | int |
| product_name | varchar |
| unit_price | int |
+--------------+---------+
product_id 是这张表的主键
Table: Sales
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| seller_id | int |
| product_id | int |
| buyer_id | int |
| sale_date | date |
| quantity | int |
| price | int |
+------ ------+---------+
这个表没有主键,它可以有重复的行.
product_id 是 Product 表的外键.
编写一个 SQL 查询,查询购买了 S8 手机却没有购买 iPhone 的买家。注意这里 S8 和 iPhone 是 Product 表中的产品。
查询结果格式如下图表示:
Product table:
+------------+--------------+------------+
| product_id | product_name | unit_price |
+------------+--------------+------------+
| 1 | S8 | 1000 |
| 2 | G4 | 800 |
| 3 | iPhone | 1400 |
+------------+--------------+------------+
Sales table:
+-----------+------------+----------+------------+----------+-------+
| seller_id | product_id | buyer_id | sale_date | quantity | price |
+-----------+------------+----------+------------+----------+-------+
| 1 | 1 | 1 | 2019-01-21 | 2 | 2000 |
| 1 | 2 | 2 | 2019-02-17 | 1 | 800 |
| 2 | 1 | 3 | 2019-06-02 | 1 | 800 |
| 3 | 3 | 3 | 2019-05-13 | 2 | 2800 |
+-----------+------------+----------+------------+----------+-------+
Result table:
+-------------+
| buyer_id |
+-------------+
| 1 |
+-------------+
上sql,拿去即可运行:
sale表:
/*
Navicat Premium Data Transfer
Source Server : 我的mysql
Source Server Type : MySQL
Source Server Version : 50736
Source Host : 8.136.255.28:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 50736
File Encoding : 65001
Date: 12/01/2022 18:16:33
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for sales
-- ----------------------------
DROP TABLE IF EXISTS `sales`;
CREATE TABLE `sales` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`seller_id` int(10) DEFAULT NULL,
`product_id` int(10) DEFAULT NULL,
`buyer_id` int(10) DEFAULT NULL,
`sale_date` datetime(0) DEFAULT NULL,
`quantity` int(255) DEFAULT NULL,
`price` int(10) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of sales
-- ----------------------------
INSERT INTO `sales` VALUES (1, 1, 1, 1, '2022-01-12 17:54:54', 2, 2000);
INSERT INTO `sales` VALUES (2, 1, 2, 2, '2022-01-12 17:54:57', 1, 800);
INSERT INTO `sales` VALUES (3, 2, 1, 3, '2022-01-12 17:54:59', 1, 800);
INSERT INTO `sales` VALUES (4, 3, 3, 3, '2022-01-12 17:55:01', 2, 2800);
SET FOREIGN_KEY_CHECKS = 1;
product表:
/*
Navicat Premium Data Transfer
Source Server : 我的mysql
Source Server Type : MySQL
Source Server Version : 50736
Source Host : 8.136.255.28:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 50736
File Encoding : 65001
Date: 12/01/2022 18:16:42
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for product
-- ----------------------------
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`product_id` int(10) DEFAULT NULL,
`product_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`unit_price` int(10) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of product
-- ----------------------------
INSERT INTO `product` VALUES (1, 1, 's8', 1000);
INSERT INTO `product` VALUES (2, 2, 'g4', 800);
INSERT INTO `product` VALUES (3, 3, 'iphone', 1400);
SET FOREIGN_KEY_CHECKS = 1;
SELECT
t.buyer_id
FROM
( SELECT s.buyer_id FROM `sales` AS s WHERE s.product_id = ( SELECT product_id FROM `product` WHERE product_name = 's8' ) ) t
WHERE
t.buyer_id NOT IN ( SELECT s.buyer_id FROM `sales` AS s WHERE s.product_id = ( SELECT product_id FROM `product` WHERE product_name = 'iphone' ) )
大佬的自动忽略,需要的cv运行一下,感受会不一样
我要刷100道算法题,第94道