leetcode1083. 销售分析 II(SQL必会呀)
问题描述:
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 TransferSource Server : 我的mysqlSource Server Type : MySQLSource Server Version : 50736Source Host : 8.136.255.28:3306Source Schema : testTarget Server Type : MySQLTarget Server Version : 50736File Encoding : 65001Date: 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 TransferSource Server : 我的mysqlSource Server Type : MySQLSource Server Version : 50736Source Host : 8.136.255.28:3306Source Schema : testTarget Server Type : MySQLTarget Server Version : 50736File Encoding : 65001Date: 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;
SELECTt.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
WHEREt.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道
leetcode1083. 销售分析 II(SQL必会呀)相关推荐
- leetcode1083. 销售分析 II(SQL)
Table: Product +--------------+---------+ | Column Name | Type | +--------------+---------+ | pr ...
- 数据库:学好SQL必须知道的10个高级概念
今天给大家分享学好SQL必须知道的10个高级概念. 1.常见表表达式(CTEs) 如果您想要查询子查询,那就是CTEs施展身手的时候 - CTEs基本上创建了一个临时表. 使用常用表表达式(CTEs) ...
- GitHub#SQL#:SQL必知必会
https://github.com/CyC2018/Interview-Notebook 一.基础 二.创建表 三.修改表 四.插入 五.更新 六.删除 七.查询 八.排序 九.过滤 十.通配符 十 ...
- 【SQL必知必会笔记(3)】SELECT语句的WHERE子句数据过滤操作
上个笔记主要介绍了利用SELECT语句检索单个/多个/所有列,并利用DISTINCT关键字检索具有唯一性的值.利用LIMIT/OFFSET子句限制结果:以及利用ORDER BY子句排序检索出的数据,主 ...
- SQL必知必会(一)SQL基础篇
SQL基础 1.SQL语言 2.SQL开发规范 3.DB.DBS 和 DBMS 的区别是什么 4.SQL执行顺序 1.oracle中执行顺序 2.MYSQL执行顺序 3.sql关键字执行顺序 5. I ...
- sql 数据库前两列值乘_SQL | SQL 必知必会笔记 (一 )
原文:SQL | SQL 必知必会笔记 (一 ) 作者: PyStaData 基本概念 一些规则 多条 SQL 语句必须以分号分隔. SQL 不区分大小写,但一般习惯关键字用大写,列名和表名使用小写. ...
- 1.《SQL必知必会》第五版 附录A样例表的添加
mysql+workbench的初步使用 一.安装mysql+workbench 二.导入附录A的样例表 最近工作需要学习SQL,查阅了相关知识购买了图书<SQL必知必会>第五版.这本书没 ...
- SQL必知必会读书笔记
<SQL必知必会> 1.SQL概述(概念.优点.数据库术语) * 日常生活中的数据库案例举例(例如在网站搜索东西:登录账号密码:取钱:) * 数据库概念:数据库database(以一种有组 ...
- SQL必知必会第4版读书笔记
SQL必知必会_4 前言 @author 鲁伟林 在读电子版<<SQL必知必会>> 第4版时,做了下笔记.供以后自己或者其他学习者参考. 电子版<<SQL必知必会& ...
最新文章
- windows time 服务无法启动 错误1058 解决方法
- jenkins简单部署
- HTML与CSS基础之常用选择器(一)
- SparkSQL 之 Shuffle Join 内核原理及应用深度剖析-Spark商业源码实战
- python123输出hello world_Python基础:输入与输出
- yyyy-MM-dd HH:mm:ss和yyyy-MM-dd hh:mm:ss的区别
- 百度语音合成data:audio/x-mpeg;base64转mp3
- 计算机桌面如何分区,展示电脑如何分区
- Win10使用以前的图片查看器
- 国风PPT自制卷轴动效,你的PPT也能很“香“
- mysql的sock文件_如何查找 mysql 的mysql.sock文件?
- java:文本框的简单使用
- 我想给我的公众号改个名,好不好嘞
- 【流体机械原理及设计03】
- 全国省市区建表sql
- kali下经典的ddos攻击软件_Kali-DDoS工具集合
- NOIP2015提高组 信息传递(图论)
- c语言cfile用法,MFC CFile类读写文件详解
- css3圣诞节喜庆文字祝福代码
- jupyter中关于pandas的dataframe行列显示不全与复原