-- 修改emp2表中Steven的id为7, 年龄改为25:
mysql>UPDATE emp2
SET id =7, age =25WHERE id =100;
Query OK,1row affected (0.00 sec)Rowsmatched: 1 Changed: 1Warnings: 0-- 查看emp2表中的数据:
mysql>SELECT*FROM emp2;+------+--------+------+| id | name | age |+------+--------+------+|...|...|...|-- 省略|7| Steven |25|+------+--------+------+7rowsinset(0.00 sec)
-- 删除指定的数据:-- 先查看一番需要删除的数据, 进行确定
mysql>SELECT*FROM emp2 WHERE id =7;+------+--------+------+| id | name | age |+------+--------+------+|7| Steven |25|+------+--------+------+1rowinset(0.00 sec)-- 删除id为7的数据:
mysql>DELETEFROM emp2 WHERE id =7;
Query OK,1row affected (0.00 sec)-- 查看emp2表中的数据:
mysql>SELECT*FROM emp2;+------+------+------+| id | name | age |+------+------+------+|1| kid |18||2| qq |18||3| qz |18||3| ez |NULL||5| vn |19||6| js |20|+------+------+------+6rowsinset(0.00 sec)
-- 创建表格:
mysql>CREATETABLE tb1 (
id INT,
a INT,
b INT,
c INT GENERATED ALWAYS AS(a + b) VIRTUAL -- 设置计算列);
Query OK,0rows affected (0.02 sec)-- 插入数据:
mysql>INSERTINTO tb1 (id, a, b)VALUES(1,1,2);
Query OK,1row affected (0.01 sec)-- 查看表中数据:
mysql>select*from tb1;+------+------+------+------+| id | a | b | c |+------+------+------+------+|1|1|2|3|+------+------+------+------+1rowinset(0.00 sec)
-- 6. 将名称为EmmaT的书的价格改为40, 并将说明改为drama.-- 查看名称为EmmaT的书籍信息:
mysql>SELECT price, note FROM books WHERE name ='EmmaT';+-------+------+| price | note |+-------+------+|35| joke |+-------+------+1rowinset(0.00 sec)-- 更新数据:
mysql>UPDATE books SET price =40, note ='drama'WHERE name ='EmmaT';
Query OK,1row affected (0.00 sec)Rowsmatched: 1 Changed: 1Warnings: 0-- 查看名称为EmmaT的书籍信息:
mysql>SELECT price, note FROM books WHERE name ='EmmaT';+-------+-------+| price | note |+-------+-------+|40| drama |+-------+-------+1rowinset(0.00 sec)-- 提交事务:
mysql>COMMIT;
Query OK,0rows affected (0.01 sec)
-- 7. 删除库存为0的记录:-- 查看库存为0的记录:
mysql>SELECT*FROM books WHERE num =0;+------+---------------+--------------+-------+---------+-------+------+| id | NAME | authors | price | pubdate | note | num |+------+---------------+--------------+-------+---------+-------+------+|3| Story of Jane | Jane Tim |45|2001| novel |0||5| Old land | Honore Blade |30|2010| law |0|+------+---------------+--------------+-------+---------+-------+------+2rowsinset(0.00 sec)-- 删除:
mysql>DELETEFROM books WHERE num =0;
Query OK,2rows affected (0.00 sec)-- 查看库存为0的记录:
mysql>SELECT*FROM books WHERE num =0;
Empty set(0.00 sec)-- 提交事务:
mysql>COMMIT;
Query OK,0rows affected (0.01 sec)
-- 8. 统计书名中包含a字母的书:
mysql>SELECTCOUNT(name)FROM books WHERE name LIKE'%a%';+-------------+|COUNT(name)|+-------------+|4|+-------------+1rowinset(0.00 sec)
-- 11.查询图书信息, 按照库存量降序排列, 如果库存量相同的按照note升序排列:
mysql>SELECT*FROM books ORDERBY num DESC, note;+------+------------+-----------------+-------+---------+----------+------+| id | NAME | authors | price | pubdate | note | num |+------+------------+-----------------+-------+---------+----------+------+|6| The Battle | Upton Sara |30|1999| medicine |40||4| Lovey Day| George Byron |25|2005| novel |30||7| Rose Hood | Richard haggard |28|2008| cartoon |28||2| EmmaT | Jane lura |40|1993| drama |22||1| Tal of AAA | Dickes |28|1995| novel |11|+------+------------+-----------------+-------+---------+----------+------+5rowsinset(0.01 sec)
-- 12. 按照note分类统计书的数量:
mysql>SELECT note,COUNT(*)FROM books GROUPBY note;+----------+----------+| note |COUNT(*)|+----------+----------+| novel |2|| drama |1|| medicine |1|| cartoon |1|+----------+----------+4rowsinset(0.00 sec)
-- 16. 查询书名达到10个字符的书, 不包括里面的空格:
mysql>SELECT NAME FROM books WHERE CHAR_LENGTH(name)>=10AND name NOTlike'% %';
Empty set(0.00 sec)
-- 17. 查询书名和类型, 其中note值为novel显示小说, law显示法律, medicine显示医药, cartoon显示卡通, joke显示笑话:
mysql>SELECT NAME, note,CASE note
WHEN'novel'THEN'小说'WHEN'law'THEN'法律'WHEN'medicine'THEN'医药'WHEN'cartoon'THEN'卡通'WHEN'joke'THEN'笑话'ENDAS`类型`FROM books;+------------+----------+------+| NAME | note | 类型 |+------------+----------+------+| Tal of AAA | novel | 小说 || EmmaT | drama |NULL|| Lovey Day| novel | 小说 || The Battle | medicine | 医药 || Rose Hood | cartoon | 卡通 |+------------+----------+------+5rowsinset(0.00 sec)
-- 18. 查询书名, 库存, 其中num值超过30本的, 显示滞销, 大于0并低于10的, 显示畅销, 为0的显示无货, 否则显示其他:
mysql>SELECT name, num,CASEWHEN num >30THEN'滞销'WHEN num >0AND num <10THEN'畅销'-- 不能使用 0 < num < 10;WHEN num =0THEN'无货'ELSE'其他'ENDAS`库存`FROM books;+------------+------+------+| name | num | 库存 |+------------+------+------+| Tal of AAA |11| 其他 || EmmaT |22| 其他 || Lovey Day|30| 其他 || The Battle |40| 滞销 || Rose Hood |28| 其他 |+------------+------+------+5rowsinset(0.00 sec)
-- 19. 统计每一种note的库存量, 并合计总量:
mysql>SELECT note,SUM(num)FROM books
GROUPBY note
WITH ROLLUP;+----------+----------+| note |SUM(num)|+----------+----------+| cartoon |28|| drama |22|| medicine |40|| novel |41||NULL|131|-- 不想显示NULL+----------+----------+5rowsinset(0.00 sec)
mysql>SELECT IFNULL(note,'合计总库存量')AS note,SUM(num)-- note为NULL显示合计总库存量FROM books
GROUPBY note
WITH ROLLUP;+--------------+----------+| note |SUM(num)|+--------------+----------+| cartoon |28|| drama |22|| medicine |40|| novel |41|| 合计总库存量 |131|+--------------+----------+5rowsinset,1 warning (0.00 sec)
-- 21. 统计库存量前三名的图书:
mysql>SELECT name, num FROM books ORDERBY num DESCLIMIT3;+------------+------+| name | num |+------------+------+| The Battle |40|| Lovey Day|30|| Rose Hood |28|+------------+------+3rowsinset(0.00 sec)
-- 22. 找出最早出版的一本书:
mysql>SELECT name, pubdate FROM books ORDERBY pubdate LIMIT1;+-------+---------+| name | pubdate |+-------+---------+| EmmaT |1993|+-------+---------+1rowinset(0.00 sec)
-- 23 找出novel中价格最高的一本书:
mysql>SELECT name, note, price FROM books
WHERE note ='novel'ORDERBY price
LIMIT1;+-----------+-------+-------+| name | note | price |+-----------+-------+-------+| Lovey Day| novel |25|+-----------+-------+-------+1rowinset(0.00 sec)
-- 24. 找出书名中字数最多的一本书, 不含空格:-- 使用REPLACE()函数将空格替换掉:
mysql>SELECT NAME,REPLACE(name,' ','')AS name2,
CHAR_LENGTH(REPLACE(name,' ',''))AS'name_length'FROM books
ORDERBY name_length;+------------+-----------+-------------+| NAME | name2 | name_length |+------------+-----------+-------------+| EmmaT | EmmaT |5|| Tal of AAA | TalofAAA |8|| Lovey Day| LoveyDay |8|| Rose Hood | RoseHood |8|| The Battle | TheBattle |9|+------------+-----------+-------------+5rowsinset(0.00 sec)-- 过滤出最长的:
mysql>SELECT NAME,REPLACE(name,' ','')AS name2,
CHAR_LENGTH(REPLACE(name,' ',''))AS'name_length'FROM books
ORDERBY name_length DESCLIMIT1;+------------+-----------+-------------+| NAME | name2 | name_length |+------------+-----------+-------------+| The Battle | TheBattle |9|+------------+-----------+-------------+1rowinset(0.00 sec)