上篇文章中,我们已经导入了book库,如果你还没有导入book库,参考:【跟晓月学数据库】使用MySQLdump 对数据导入导出
这篇文章,主要是基于book库的操作,希望对你有用。
个人主页:我是沐风晓月
个人简介:大家好,我是沐风晓月,阿里云社区专家博主
座右铭: 先努力成长自己,再帮助更多的人 ,一起加油进步
欢迎大家:这里是CSDN,我总结知识的地方,喜欢的话请三连,有问题请私信
[root@mufeng41 ~]# mysql -p'Root!2#admin'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.42 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| book |
| mysql |
| performance_schema |
| sys |
| test01 |
| test_db |
+--------------------+
7 rows in set (0.00 sec)
mysql>
mysql> use book
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from category;
+---------+---------------+
| bTypeId | bTypeName |
+---------+---------------+
| 1 | windows应用 |
| 2 | 网站 |
| 3 | 3D动画 |
| 4 | linux学习 |
| 5 | Delphi学习 |
| 6 | 黑客 |
| 7 | 网络技术 |
| 8 | 安全 |
| 9 | 平面 |
| 10 | AutoCAD技术 |
+---------+---------------+
10 rows in set (0.00 sec)
查看表结构
mysql> desc books;
+------------+------------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------------------------------------+------+-----+---------+----------------+
| bId | int(4) | NO | PRI | NULL | auto_increment |
| bName | varchar(255) | YES | | NULL | |
| bTypeId | enum('1','2','3','4','5','6','7','8','9','10') | YES | | NULL | |
| publishing | varchar(255) | YES | | NULL | |
| price | int(4) | YES | | NULL | |
| pubDate | date | YES | | NULL | |
| author | varchar(30) | YES | | NULL | |
| ISBN | varchar(255) | YES | | NULL | |
+------------+------------------------------------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
mysql> desc category;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| bTypeId | int(4) | NO | PRI | NULL | auto_increment |
| bTypeName | varchar(40) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql>
mysql> SELECT bName, author
-> FROM books
-> WHERE price > 50;
SELECT bName, author, pubDate
FROM books
WHERE publishing = '电脑爱好者杂志社';
SELECT COUNT(*) AS book_count
FROM books
WHERE bTypeId IN (SELECT bTypeId FROM category WHERE bTypeName = '网站');
SELECT c.bTypeName, AVG(b.price) AS avg_price
FROM category c
JOIN books b ON c.bTypeId = b.bTypeId
GROUP BY c.bTypeName
ORDER BY avg_price DESC;
SELECT b.bName, b.pubDate, c.bTypeName
FROM books b
JOIN category c ON b.bTypeId = c.bTypeId
WHERE b.pubDate BETWEEN '2021-01-01' AND '2021-12-31' AND c.bTypeName = '网站';
mysql> SELECT bName, price, publishing
-> FROM books
-> WHERE price BETWEEN 40 AND 50 AND publishing = '航空工业出版社';
+-----------------------+-------+-----------------------+
| bName | price | publishing |
+-----------------------+-------+-----------------------+
| 黑客与网络安全 | 41 | 航空工业出版社 |
+-----------------------+-------+-----------------------+
1 row in set (0.00 sec)
bTypeId:4
bTypeName:linux技术
bName=linux大全
publishing:互联网老辛出品
author:沐风晓月
pubDate:2023-03-27
price:88
ISBN:200001
INSERT INTO books (bTypeId, bName, publishing, price, pubDate, author, ISBN)
VALUES (4, 'linux大全', '互联网老辛出品', 88, '2023-03-27', '沐风晓月', '200001');
mysql> SELECT bName, price, pubDate
-> FROM books
-> WHERE author = '沐风晓月';
+--------------+-------+------------+
| bName | price | pubDate |
+--------------+-------+------------+
| 填写书名 | 88 | 2023-03-27 |
| linux大全 | 88 | 2023-03-27 |
+--------------+-------+------------+
2 rows in set (0.01 sec)
SELECT b.bName, b.author, p.publishing
FROM books b
RIGHT JOIN publishing p ON b.publishingId = p.publishingId;
关于MySQL的进阶实战,你学会了吗,欢迎评论区留言一起探讨。