mysql>select*fromuser; +----+----------+-----+-----+ | id | name | age | sex | +----+----------+-----+-----+ |1| zhangsan |16| M | |2| lisi |13| M | |3| laoliu |24| W | |4| jianzhe |14| W | |5| wangwei |21| M | +----+----------+-----+-----+ 5rowsinset (0.03 sec)
#Enter a name for the error log file. Otherwise a default name will be used. log-error=err.log #Enter a name for the query log file. Otherwise a default name will be used. #log= #Enter a name for the slow query log file. Otherwise a default name will be used. #log-slow-queries= #Enter a name for the update log file. Otherwise a default name will be used. #log-update= #Enter a name for the binary log. Otherwise a default name will be used. #log-bin=
create tableuser( id int unsigned primary key Auto_increment comment "id", nikename varchar(50) uniquenot null comment "昵称", age tinyint unsigned not nulldefault18, sex enum('male','female') )
root@zjz-VMware-Virtual-Platform:~# mysqldump -u root -p testcc user > ~/user.sql Enter password: root@zjz-VMware-Virtual-Platform:~# ls snap user.sql root@zjz-VMware-Virtual-Platform:~# cat user.sql -- MySQL dump 10.13 Distrib 8.0.40, for Linux (x86_64) -- -- Host: localhost Database: testcc -- ------------------------------------------------------ -- Server version 8.0.40-0ubuntu0.24.04.1
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!50503 SET NAMES utf8mb4 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
-- -- Table structure for table `user` --
DROP TABLE IF EXISTS `user`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `user` ( `name` varchar(20) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; /*!40101 SET character_set_client = @saved_cs_client */;
-- -- Dumping data for table `user` --
LOCK TABLES `user` WRITE; /*!40000 ALTER TABLE `user` DISABLE KEYS */; INSERT INTO `user` VALUES ('zhangsan'),('lisi'); /*!40000 ALTER TABLE `user` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2024-12-04 17:55:54 root@zjz-VMware-Virtual-Platform:~#
还有常用的导出:
1
mysql -u root -p -D school -e "select *from user where age>18" > ~/user.txt
zjz@zjz-VMware-Virtual-Platform:~$ cat user.sql -- MySQL dump 10.13 Distrib 8.0.40, forLinux(x86_64) -- -- Host: localhost Database: testcc -- ------------------------------------------------------ -- Server version 8.0.40-0ubuntu0.24.04.1 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!50503 SET NAMES utf8mb4 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
-- -- Table structure for table `user` -- create database testcc; use testcc;
DROP TABLE IF EXISTS `user`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `user` ( `name` varchar(20) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; /*!40101 SET character_set_client = @saved_cs_client */;
-- -- Dumping data for table `user` --
LOCK TABLES `user` WRITE; /*!40000 ALTER TABLE `user` DISABLE KEYS */; INSERT INTO `user` VALUES ('zhangsan'),('lisi'); /*!40000 ALTER TABLE `user` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2024-12-0417:55:54 zjz@zjz-VMware-Virtual-Platform:~$
select c.uid,c.name,c.age,c.sex, b.cid,b.cname,b.credit,a.time,a.score from exams a innerjoin course b on a.cid = b.cid innerjoin student c on a.uid = c.uid where a.uid =1and a.cid =2;
测试结果:
1 2 3 4 5 6 7 8 9 10 11 12
mysql>select c.uid,c.name,c.age,c.sex, b.cid,b.cname,b.credit,a.time,a.score from exams a ->innerjoin course b on a.cid = b.cid ->innerjoin student c on a.uid = c.uid ->where a.uid =1and a.cid =2; +-----+----------+-----+-----+-----+-----------------+--------+------------+-------+ | uid | name | age | sex | cid | cname | credit |time| score | +-----+----------+-----+-----+-----+-----------------+--------+------------+-------+ |1| zhangsan |18| M |2| C++高级课程 |10|2021-04-10|80| +-----+----------+-----+-----+-----+-----------------+--------+------------+-------+ 1rowinset (0.02 sec)
mysql>
如果是查询zhangsan同学的详细信息,所有选修课程的信息和对应的成绩,则sql应该如下:
1 2 3 4
select c.uid,c.name,c.age,c.sex, b.cid,b.cname,b.credit,a.time,a.score from exams a innerjoin course b on a.cid = b.cid innerjoin student c on a.uid = c.uid where a.uid =1;
测试结果:
1 2 3 4 5 6 7 8 9 10 11 12 13
mysql>select c.uid,c.name,c.age,c.sex, b.cid,b.cname,b.credit,a.time,a.score from exams a ->innerjoin course b on a.cid = b.cid ->innerjoin student c on a.uid = c.uid ->where a.uid =1; +-----+----------+-----+-----+-----+-----------------+--------+------------+-------+ | uid | name | age | sex | cid | cname | credit |time| score | +-----+----------+-----+-----+-----+-----------------+--------+------------+-------+ |1| zhangsan |18| M |1| C++基础课程 |5|2021-04-09|99| |1| zhangsan |18| M |2| C++高级课程 |10|2021-04-10|80| +-----+----------+-----+-----+-----+-----------------+--------+------------+-------+ 2rowsinset (0.02 sec)
mysql>
如果是要查看课程id为2,且成绩大于等于90的信息
1 2 3 4
select c.uid,c.name,c.age,c.sex, b.cid,b.cname,b.credit,a.time,a.score from exams a innerjoin course b on a.cid = b.cid innerjoin student c on a.uid = c.uid where a.cid =2and a.score >=90;
测试结果:
1 2 3 4 5 6 7 8 9 10 11 12 13
mysql>select c.uid,c.name,c.age,c.sex, b.cid,b.cname,b.credit,a.time,a.score from exams a ->innerjoin course b on a.cid = b.cid ->innerjoin student c on a.uid = c.uid ->where a.cid =2and a.score >=90; +-----+---------+-----+-----+-----+-----------------+--------+------------+-------+ | uid | name | age | sex | cid | cname | credit |time| score | +-----+---------+-----+-----+-----+-----------------+--------+------------+-------+ |2| gaoyang |20| W |2| C++高级课程 |10|2021-04-10|90| |3| chenwei |22| M |2| C++高级课程 |10|2021-04-10|93| +-----+---------+-----+-----+-----+-----------------+--------+------------+-------+ 2rowsinset (0.00 sec)
mysql>
通过课程分组,计算每个课程的选课人数,按降序排序
1 2 3 4
select b.cid,b.cname,b.credit,count(*) number from exams a innerjoin course b on a.cid = b.cid groupby a.cid orderby number desc;
测试结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
mysql>select b.cid,b.cname,b.credit,count(*) number from exams a ->innerjoin course b on a.cid = b.cid ->groupby a.cid ->orderby number desc; +-----+-----------------+--------+--------+ | cid | cname | credit | number | +-----+-----------------+--------+--------+ |2| C++高级课程 |10|4| |3| C++项目开发 |8|3| |4| C++算法课程 |12|3| |1| C++基础课程 |5|2| +-----+-----------------+--------+--------+ 4rowsinset (0.00 sec)
mysql>
通过课程分组,计算每个课程成绩大于等于90分的选课人数,按降序排序
1 2 3 4 5
select b.cid,b.cname,b.credit,count(*) number from exams a innerjoin course b on a.cid = b.cid where a.score >=90 groupby a.cid orderby number desc;
测试结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
mysql>select b.cid,b.cname,b.credit,count(*) number from exams a ->innerjoin course b on a.cid = b.cid ->where a.score >=90 ->groupby a.cid ->orderby number desc; +-----+-----------------+--------+--------+ | cid | cname | credit | number | +-----+-----------------+--------+--------+ |4| C++算法课程 |12|3| |2| C++高级课程 |10|2| |1| C++基础课程 |5|1| |3| C++项目开发 |8|1| +-----+-----------------+--------+--------+ 4rowsinset (0.01 sec)
mysql>
cid=2这门课程考试成绩的最高分的学生信息和课程信息
1 2 3 4 5 6 7 8 9 10
select c.uid, c.name, c.age, c.sex, b.cid, b.cname, b.credit, a.score from exams a innerjoin course b on a.cid = b.cid innerjoin student c on a.uid = c.uid where a.cid =2 and a.score = ( selectmax(score) from exams where cid =2 );
select b.cid,b.cname,b.credit,ROUND(AVG(a.score), 2) average_score from exams a innerjoin course b on a.cid = b.cid groupby a.cid orderby average_score desc;
mysql>select name,age fromusergroupby age; ERROR 1055 (42000): Expression #1ofSELECT list isnotinGROUPBY clause andcontains nonaggregated column'school.user.name' which isnot functionally dependent on columns inGROUPBY clause; this is incompatible with sql_mode=only_full_group_by mysql>
mysql>select*fromuser; +----+----------+-----+-----+ | id | name | age | sex | +----+----------+-----+-----+ |1| zhangsan |16| W | |2| lisi |21| M | |3| laoliu |24| W | |4| jianzhe |16| W | |5| wangwei |21| M | +----+----------+-----+-----+ 5rowsinset (0.00 sec)
mysql>select age fromusergroupby age; +-----+ | age | +-----+ |16| |21| |24| +-----+ 3rowsinset (0.00 sec)
mysql>select age,sum(age) fromusergroupby age having age >20; +-----+----------+ | age |sum(age) | +-----+----------+ |21|42| |24|24| +-----+----------+ 2rowsinset (0.00 sec)
mysql>select age,sum(age) fromuserwhere age >20groupby age;#推荐使用where,age有索引的话where可以使用索引 +-----+----------+ | age |sum(age) | +-----+----------+ |21|42| |24|24| +-----+----------+ 2rowsinset (0.00 sec)
mysql>select age,sex,count(*) fromusergroupby age,sex; +-----+-----+----------+ | age | sex |count(*) | +-----+-----+----------+ |16| W |2| |21| M |2| |24| W |1| +-----+-----+----------+ 3rowsinset (0.00 sec)
mysql>
mysql>select age,sex,count(id) fromusergroupby age,sex orderby age desc; +-----+-----+-----------+ | age | sex |count(id) | +-----+-----+-----------+ |24| W |1| |21| M |2| |16| W |2| +-----+-----+-----------+ 3rowsinset (0.01 sec)
mysql>
其实,从上面可以看出,其实order by内部实际上是进行了排序的,我们使用explain查看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
mysql> explain select age fromusergroupby age; +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+ |1| SIMPLE |user|NULL|ALL|NULL|NULL|NULL|NULL|5|100.00|Using temporary | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+ 1rowinset, 1 warning (0.00 sec)
mysql> explain select name fromusergroupby name; +----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-------------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-------------+ |1| SIMPLE |user|NULL| index | name | name |202|NULL|5|100.00|Using index | +----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-------------+ 1rowinset, 1 warning (0.00 sec)
mysql> explain select a.*,b.*from student a rightjoin exams b on a.uid = b.uid; +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------+ |1| SIMPLE | b |NULL|ALL|NULL|NULL|NULL|NULL|12|100.00|NULL| |1| SIMPLE | a |NULL| eq_ref |PRIMARY|PRIMARY|4| school.b.uid |1|100.00|NULL| +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------+ 2rowsinset, 1 warning (0.00 sec)
mysql> explain select a.*,b.*from student a leftjoin exams b on a.uid = b.uid; +----+-------------+-------+------------+------+---------------+---------+---------+--------------+------+----------+-------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+------+---------------+---------+---------+--------------+------+----------+-------+ |1| SIMPLE | a |NULL|ALL|NULL|NULL|NULL|NULL|6|100.00|NULL| |1| SIMPLE | b |NULL|ref|PRIMARY|PRIMARY|4| school.a.uid |2|100.00|NULL| +----+-------------+-------+------------+------+---------------+---------+---------+--------------+------+----------+-------+ 2rowsinset, 1 warning (0.00 sec)
mysql>
现在有这么一个情景:我们要查询没有考过试的学生
我们可以使用子连接
1
select*from student where uid notin (select uid from exams);
1 2 3 4 5 6 7 8 9
mysql>select*from student where uid notin (select uid from exams); +-----+--------+-----+-----+ | uid | name | age | sex | +-----+--------+-----+-----+ |6| weiwie |20| M | +-----+--------+-----+-----+ 1rowinset (0.00 sec)
mysql>select a.*from student a leftjoin exams b on a.uid = b.uid where b.cid isnull; +-----+--------+-----+-----+ | uid | name | age | sex | +-----+--------+-----+-----+ |6| weiwie |20| M | +-----+--------+-----+-----+ 1rowinset (0.01 sec)
mysql> explain select a.*from student a leftjoin exams b on a.uid = b.uid where b.cid isnull; +----+-------------+-------+------------+------+---------------+---------+---------+--------------+------+----------+--------------------------------------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+------+---------------+---------+---------+--------------+------+----------+--------------------------------------+ |1| SIMPLE | a |NULL|ALL|NULL|NULL|NULL|NULL|6|100.00|NULL | |1| SIMPLE | b |NULL|ref|PRIMARY|PRIMARY|4| school.a.uid |2|10.00|Usingwhere; Notexists; Using index | +----+-------------+-------+------------+------+---------------+---------+---------+--------------+------+----------+--------------------------------------+ 2rowsinset, 1 warning (0.01 sec)
mysql>
查看没有参加课程号为3考试的学生
1 2 3
select a.*from student a leftjoin exams b on a.uid = b.uid and b.cid =3 where b.cid isnull;
1 2 3 4 5 6 7 8 9 10 11
mysql>select a.*from student a leftjoin exams b on a.uid = b.uid and b.cid =3where b.cid isnull; +-----+----------+-----+-----+ | uid | name | age | sex | +-----+----------+-----+-----+ |1| zhangsan |18| M | |4| linfeng |21| W | |6| weiwie |20| M | +-----+----------+-----+-----+ 3rowsinset (0.01 sec)
mysql>
explain 查询
1 2 3 4 5 6 7 8 9 10 11 12
mysql> explain select a.*from student a leftjoin exams b on a.uid = b.uid and b.cid =3where b.cid isnull; +----+-------------+-------+------------+--------+---------------+---------+---------+--------------------+------+----------+--------------------------------------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+--------+---------------+---------+---------+--------------------+------+----------+--------------------------------------+ |1| SIMPLE | a |NULL|ALL|NULL|NULL|NULL|NULL|5|100.00|NULL | |1| SIMPLE | b |NULL| eq_ref |PRIMARY|PRIMARY|8| school.a.uid,const |1|100.00|Usingwhere; Notexists; Using index | +----+-------------+-------+------------+--------+---------------+---------+---------+--------------------+------+----------+--------------------------------------+ 2rowsinset, 1 warning (0.00 sec)