创建数据库test01_library

  1. create database if not exists test01_library character set 'utf8';

创建表 books,表结构如下:

image.png

  1. use test01_library;
  2. create table if not exists books (
  3. id int,
  4. `name` varchar(50),
  5. `authors` varchar(100),
  6. price float,
  7. pubdate year,
  8. note varchar(100),
  9. num int
  10. );

向books表中插入数据

image.png
要求:

  • 不指定字段名称,插入第一条记录
  • 指定所有字段名称,插入第二记录
  • 同时插入多条记录(剩下的所有记录) ```sql use test01_library;

insert into books values (1, ‘Tal of AAA’, ‘Dickes’, 23, ‘1995’, ‘novel’, 11);

insert into books(id,name,authors,price,pubdate,note,num) values(2,’EmmaT’,’Jane lura’,35,’1993’,’joke’,22);

insert into books(id,name,authors,price,pubdate,note,num) values (3,’Story of Jane’,’Jane Tim’,40,2001,’novel’,0), (4,’Lovey Day’,’George Byron’,20,2005,’novel’,30), (5,’Old land’,’Honore Blade’,30,2010,’Law’,0), (6,’The Battle’,’Upton Sara’,30,1999,’medicine’,40), (7,’Rose Hood’,’Richard haggard’,28,2008,’cartoon’,28);

  1. <a name="Ia0a9"></a>
  2. # 将小说类型(novel)的书的价格都增加5
  3. ```sql
  4. use test01_library;
  5. update books
  6. set price = price + 5
  7. where note = 'novel';

将名称为EmmaT的书的价格改为4日,并将说明改为drama

  1. use test01_library;
  2. update books
  3. set price = 40, note = 'drama'
  4. where `name` = 'EmmaT';

删除库存为0的记录

  1. use test01_library;
  2. delete from books
  3. where num = 0;

统计书名中包含a字母的书

  1. use test01_library;
  2. select `name`
  3. from books
  4. where `name` like '%a%';

统计书名中包含a字母的书的数量和库存总量

  1. use test01_library;
  2. select count(*), sum(num)
  3. from books
  4. where `name` like '%a%';

找出“novel”类型的书,按照价格降序排列

  1. use test01_library;
  2. select *
  3. from books
  4. where note = 'novel'
  5. order by price desc;

查询图书信息,按照库存量降序排列,如果库存量相同的按照note升序排列

  1. use test01_library;
  2. select *
  3. from books
  4. order by num desc, note;

按照note分类统计书的数量

  1. use test01_library;
  2. select note, count(*)
  3. from books
  4. group by note;

按照note分类统计书的库存量,显示库存量超过30本的

  1. use test01_library;
  2. select note, sum(num)
  3. from books
  4. group by note
  5. having sum(num) > 30;

查询所有图书,每页显示5本,显示第二页

  1. use test01_library;
  2. select *
  3. from books
  4. limit 5,5;

按照note分类统计书的库存量,显示库存量最多的

  1. use test01_library;
  2. select note, sum(num)
  3. from books
  4. group by note
  5. having sum(num) >= all (
  6. select sum(num)
  7. from books
  8. group by note
  9. );
  10. # 或者
  11. select note, sum(num)
  12. from books
  13. group by note
  14. order by sum(num) desc
  15. limit 0,1;

查询书名达到10个字符的书,不包括里面的空格

  1. use test01_library;
  2. select `name`
  3. from books
  4. where length(replace(`name`,' ', '')) >= 10;

查询书名和类型,其中note值为novel显示小说,law显示法律,medicine显示医药

  1. use test01_library;
  2. select `name`, note, case note
  3. when 'novel' then '小说'
  4. when 'law' then '法律'
  5. when 'medicine' then '医药'
  6. when 'cartoon' then '卡通'
  7. when 'joke' then '笑话'
  8. else '其它'
  9. end '类型'
  10. from books;

查询书名、库存,其中num值超过30本的,显示滞销,大于0并低于10的,显示畅销,为0的显示需要无货

  1. use test01_library;
  2. SELECT NAME AS "书名",num AS "库存", CASE WHEN num > 30 THEN '滞销'
  3. WHEN num > 0 AND num < 10 THEN '畅销'
  4. WHEN num = 0 THEN '无货'
  5. ELSE '正常'
  6. END "显示状态"
  7. FROM books;

统计每一种note的库存量,并合计总量

  1. SELECT IFNULL(note,'合计库存总量') AS note,SUM(num)
  2. FROM books
  3. GROUP BY note WITH ROLLUP;

统计库存量前三名的图书

  1. use test01_library;
  2. select *
  3. from books
  4. order by num desc
  5. limit 0,3;

找出最早出版的一本书

  1. use test01_library;
  2. select *
  3. from books
  4. order by pubdate
  5. limit 0,1;

找出novel中价格最高的一本书

  1. use test01_library;
  2. select *
  3. from books
  4. where note = 'novel'
  5. order by price desc
  6. limit 0,1;

找出书名中字数最多的一本书,不含空格

  1. use test01_library;
  2. SELECT *
  3. FROM books
  4. ORDER BY CHAR_LENGTH(REPLACE(NAME,' ','')) DESC
  5. LIMIT 0,1;