1 Star 0 Fork 0

逸扬/leetcode-hub-mysql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc-1164.test 1.50 KB
一键复制 编辑 原始数据 按行查看 历史
# $1164. 指定日期的产品价格
# https://leetcode-cn.com/problems/product-price-at-a-given-date/
# SQL架构
Create table If Not Exists Products (product_id int, new_price int, change_date date);
Truncate table Products;
insert into Products (product_id, new_price, change_date) values ('1', '20', '2019-08-14');
insert into Products (product_id, new_price, change_date) values ('2', '50', '2019-08-14');
insert into Products (product_id, new_price, change_date) values ('1', '30', '2019-08-15');
insert into Products (product_id, new_price, change_date) values ('1', '35', '2019-08-16');
insert into Products (product_id, new_price, change_date) values ('2', '65', '2019-08-17');
insert into Products (product_id, new_price, change_date) values ('3', '20', '2019-08-18');
# Write your MySQL query statement below
select
tmp1.product_id as product_id,
ifnull(tmp2.new_price, 10) as price
from
(
select
distinct product_id
from
Products
) tmp1
left join (
select
product_id,
new_price
from
Products
where
(product_id, change_date) in (
select
product_id,
max(change_date)
from
Products
where
change_date <= '2019-08-16'
group by
product_id
)
) tmp2 on tmp1.product_id = tmp2.product_id;
# clean-up
drop table Products;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gdut_yy/leetcode-hub-mysql.git
git@gitee.com:gdut_yy/leetcode-hub-mysql.git
gdut_yy
leetcode-hub-mysql
leetcode-hub-mysql
master

搜索帮助