问题描述:
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查询,报告2019年春季才售出的产品。即仅在2019-01-01至2019-03-31(含)之间出售的商品。
查询结果格式如下所示:
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 | 2 | 3 | 2019-06-02 | 1 | 800 |
| 3 | 3 | 4 | 2019-05-13 | 2 | 2800 |
+-----------+------------+----------+------------+----------+-------+
Result table:
+-------------+--------------+
| product_id | product_name |
+-------------+--------------+
| 1 | S8 |
+-------------+--------------+
id为1的产品仅在2019年春季销售,其他两个产品在之后销售。
sql语句拿去即可运行:
product:
/*
Navicat Premium Data Transfer
Source Server : 本地mysql
Source Server Type : MySQL
Source Server Version : 80025
Source Host : localhost:3306
Source Schema : study
Target Server Type : MySQL
Target Server Version : 80025
File Encoding : 65001
Date: 12/01/2022 21:36:40
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for product
-- ----------------------------
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`product_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`product_id` int(0) DEFAULT NULL,
`unit_price` decimal(10, 2) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of product
-- ----------------------------
INSERT INTO `product` VALUES (1, 's8', 1, 1000.00);
INSERT INTO `product` VALUES (2, 'g4', 2, 800.00);
INSERT INTO `product` VALUES (3, 'iphone', 3, 1400.00);
SET FOREIGN_KEY_CHECKS = 1;
sales表:
/*
Navicat Premium Data Transfer
Source Server : 本地mysql
Source Server Type : MySQL
Source Server Version : 80025
Source Host : localhost:3306
Source Schema : study
Target Server Type : MySQL
Target Server Version : 80025
File Encoding : 65001
Date: 12/01/2022 21:36:48
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for sales
-- ----------------------------
DROP TABLE IF EXISTS `sales`;
CREATE TABLE `sales` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`product_id` int(0) DEFAULT NULL,
`buyer_id` int(0) DEFAULT NULL,
`quantity` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`price` decimal(10, 2) DEFAULT NULL,
`seller_id` int(0) DEFAULT NULL,
`sale_date` datetime(0) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of sales
-- ----------------------------
INSERT INTO `sales` VALUES (1, 1, 1, '2', 2000.00, 1, '2019-01-21 21:21:33');
INSERT INTO `sales` VALUES (2, 2, 2, '1', 800.00, 1, '2019-02-17 21:22:14');
INSERT INTO `sales` VALUES (3, 2, 3, '1', 800.00, 2, '2019-06-02 21:22:50');
INSERT INTO `sales` VALUES (4, 3, 4, '2', 2800.00, 3, '2019-05-13 21:23:35');
SET FOREIGN_KEY_CHECKS = 1;
select product_id,product_name
from Product
where product_id in (SELECT s.product_id FROM `sales` s WHERE s.sale_date BETWEEN '2019-01-01' and '2019-03-31')
运行结果:
大佬的自动忽略,需要的cv运行一下,感受会不一样
我要刷100道算法题,第95道