赞
踩
Create table If Not Exists Books (book_id int, name varchar(50), available_from date) Create table If Not Exists Orders (order_id int, book_id int, quantity int, dispatch_date date) Truncate table Books insert into Books (book_id, name, available_from) values ('1', 'Kalila And Demna', '2010-01-01') insert into Books (book_id, name, available_from) values ('2', '28 Letters', '2012-05-12') insert into Books (book_id, name, available_from) values ('3', 'The Hobbit', '2019-06-10') insert into Books (book_id, name, available_from) values ('4', '13 Reasons Why', '2019-06-01') insert into Books (book_id, name, available_from) values ('5', 'The Hunger Games', '2008-09-21') Truncate table Orders insert into Orders (order_id, book_id, quantity, dispatch_date) values ('1', '1', '2', '2018-07-26') insert into Orders (order_id, book_id, quantity, dispatch_date) values ('2', '1', '1', '2018-11-05') insert into Orders (order_id, book_id, quantity, dispatch_date) values ('3', '3', '8', '2019-06-11') insert into Orders (order_id, book_id, quantity, dispatch_date) values ('4', '4', '6', '2019-06-05') insert into Orders (order_id, book_id, quantity, dispatch_date) values ('5', '4', '5', '2019-06-20') insert into Orders (order_id, book_id, quantity, dispatch_date) values ('6', '5', '9', '2009-02-02') insert into Orders (order_id, book_id, quantity, dispatch_date) values ('7', '5', '8', '2010-04-13')
书籍表 Books
:
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| book_id | int |
| name | varchar |
| available_from | date |
+----------------+---------+
book_id 是这个表的主键。
订单表 Orders
:
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| order_id | int |
| book_id | int |
| quantity | int |
| dispatch_date | date |
+----------------+---------+
order_id 是这个表的主键。
book_id 是 Books 表的外键。
你需要写一段 SQL 命令,筛选出过去一年中订单总量 少于10本 的 书籍 。
注意:不考虑 上架(available from)距今 不满一个月 的书籍。并且 假设今天是 2019-06-23 。
下面是样例输出结果:
Books 表: +---------+--------------------+----------------+ | book_id | name | available_from | +---------+--------------------+----------------+ | 1 | "Kalila And Demna" | 2010-01-01 | | 2 | "28 Letters" | 2012-05-12 | | 3 | "The Hobbit" | 2019-06-10 | | 4 | "13 Reasons Why" | 2019-06-01 | | 5 | "The Hunger Games" | 2008-09-21 | +---------+--------------------+----------------+ Orders 表: +----------+---------+----------+---------------+ | order_id | book_id | quantity | dispatch_date | +----------+---------+----------+---------------+ | 1 | 1 | 2 | 2018-07-26 | | 2 | 1 | 1 | 2018-11-05 | | 3 | 3 | 8 | 2019-06-11 | | 4 | 4 | 6 | 2019-06-05 | | 5 | 4 | 5 | 2019-06-20 | | 6 | 5 | 9 | 2009-02-02 | | 7 | 5 | 8 | 2010-04-13 | +----------+---------+----------+---------------+ Result 表: +-----------+--------------------+ | book_id | name | +-----------+--------------------+ | 1 | "Kalila And Demna" | | 2 | "28 Letters" | | 5 | "The Hunger Games" | +-----------+--------------------+
题解一
select temp1.book_id, temp1.name from (
select book_id, name from books
where datediff('2019-06-23', available_from) > 30
) as temp1 left join (
select book_id, sum(quantity) as total_sales from orders
where datediff('2019-06-23', dispatch_date) < 365
group by book_id
) as temp2 on temp1.book_id = temp2.book_id
where ifnull(total_sales, 0) < 10;
题解二
select books.book_id, books.name from books left join orders
on books.book_id = orders.book_id
and ifnull(datediff('2019-06-23', dispatch_date), 0) < 365
where datediff('2019-06-23', available_from) > 30
group by books.book_id, name having ifnull(sum(quantity), 0) < 10;
题解三
select temp1.book_id, temp1.name from (
select book_id, name from books
where datediff('2019-06-23', available_from) > 30
) as temp1 left join orders on temp1.book_id = orders.book_id
and ifnull(datediff('2019-06-23', dispatch_date), 0) < 365
group by temp1.book_id, temp1.name having ifnull(sum(quantity), 0) < 10;
Create table If Not Exists Traffic (user_id int, activity ENUM('login', 'logout', 'jobs', 'groups', 'homepage'), activity_date date) Truncate table Traffic insert into Traffic (user_id, activity, activity_date) values ('1', 'login', '2019-05-01') insert into Traffic (user_id, activity, activity_date) values ('1', 'homepage', '2019-05-01') insert into Traffic (user_id, activity, activity_date) values ('1', 'logout', '2019-05-01') insert into Traffic (user_id, activity, activity_date) values ('2', 'login', '2019-06-21') insert into Traffic (user_id, activity, activity_date) values ('2', 'logout', '2019-06-21') insert into Traffic (user_id, activity, activity_date) values ('3', 'login', '2019-01-01') insert into Traffic (user_id, activity, activity_date) values ('3', 'jobs', '2019-01-01') insert into Traffic (user_id, activity, activity_date) values ('3', 'logout', '2019-01-01') insert into Traffic (user_id, activity, activity_date) values ('4', 'login', '2019-06-21') insert into Traffic (user_id, activity, activity_date) values ('4', 'groups', '2019-06-21') insert into Traffic (user_id, activity, activity_date) values ('4', 'logout', '2019-06-21') insert into Traffic (user_id, activity, activity_date) values ('5', 'login', '2019-03-01') insert into Traffic (user_id, activity, activity_date) values ('5', 'logout', '2019-03-01') insert into Traffic (user_id, activity, activity_date) values ('5', 'login', '2019-06-21') insert into Traffic (user_id, activity, activity_date) values ('5', 'logout', '2019-06-21')
Traffic
表:
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user_id | int |
| activity | enum |
| activity_date | date |
+---------------+---------+
该表没有主键,它可能有重复的行。
activity 列是 ENUM 类型,可能取 ('login', 'logout', 'jobs', 'groups', 'homepage') 几个值之一。
编写一个 SQL 查询,以查询从今天起最多 90 天内,每个日期该日期首次登录的用户数。假设今天是 2019-06-30.
查询结果格式如下例所示:
Traffic 表: +---------+----------+---------------+ | user_id | activity | activity_date | +---------+----------+---------------+ | 1 | login | 2019-05-01 | | 1 | homepage | 2019-05-01 | | 1 | logout | 2019-05-01 | | 2 | login | 2019-06-21 | | 2 | logout | 2019-06-21 | | 3 | login | 2019-01-01 | | 3 | jobs | 2019-01-01 | | 3 | logout | 2019-01-01 | | 4 | login | 2019-06-21 | | 4 | groups | 2019-06-21 | | 4 | logout | 2019-06-21 | | 5 | login | 2019-03-01 | | 5 | logout | 2019-03-01 | | 5 | login | 2019-06-21 | | 5 | logout | 2019-06-21 | +---------+----------+--------
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。