mysql进阶
mysql二进制安装
root@linux-1 ~]# cd /usr/src/
[root@linux-1 src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
--2022-07-26 21:01:57-- https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 96.7.99.160, 2600:140e:6:9bc::2e31, 2600:140e:6:9a3::2e31
Connecting to downloads.mysql.com (downloads.mysql.com)|96.7.99.160|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz [following]
--2022-07-26 21:01:58-- https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
Resolving cdn.mysql.com (cdn.mysql.com)... 23.194.211.12
Connecting to cdn.mysql.com (cdn.mysql.com)|23.194.211.12|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 674830866 (644M) [application/x-tar-gz]
Saving to: 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz'
mysql-5.7.38-linux 100%[==============>] 643.57M 9.39MB/s in 68s
2022-07-26 21:03:07 (9.49 MB/s) - 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz' saved [674830866/674830866]
[root@linux-1 src]# ls
debug kernels mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@linux-1 src]#
配置环境变量
[root@linux-1 src]# useradd -M -r -s /sbin/nologin mysql //创建用户和组,
[root@linux-1 src]# id mysql
uid=993(mysql) gid=991(mysql) groups=991(mysql)
[root@linux-1 src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ //解压下载的压缩包,并指定路径。
[root@linux-1 src]# cd /usr/local/ //进入指定的压缩路径
[root@linux-1 local]# ls
apache2 games lib64 sbin
bin include libexec share
etc lib mysql-5.7.38-linux-glibc2.12-x86_64 src
[root@linux-1 local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64 mysql //给mysql的目录做一个软链接,方便以后使用
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64'
[root@linux-1 local]# ll
total 0
drwxr-xr-x. 14 root root 164 Jul 12 22:55 apache2
drwxr-xr-x. 2 root root 6 Jun 22 2021 bin
drwxr-xr-x. 2 root root 6 Jun 22 2021 etc
drwxr-xr-x. 2 root root 6 Jun 22 2021 games
drwxr-xr-x. 2 root root 6 Jun 22 2021 include
drwxr-xr-x. 2 root root 6 Jun 22 2021 lib
drwxr-xr-x. 3 root root 17 Jul 11 21:05 lib64
drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec
lrwxrwxrwx. 1 root root 35 Jul 27 01:59 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 9 root root 129 Jul 27 01:57 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Jun 22 2021 sbin
drwxr-xr-x. 5 root root 49 Jul 11 21:05 share
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
[root@linux-1 local]#
[root@linux-1 local]# chown -R mysql.mysql mysql* //改一下文件的属组属主。
[root@linux-1 local]# ll
total 0
drwxr-xr-x. 14 root root 164 Jul 12 22:55 apache2
drwxr-xr-x. 2 root root 6 Jun 22 2021 bin
drwxr-xr-x. 2 root root 6 Jun 22 2021 etc
drwxr-xr-x. 2 root root 6 Jun 22 2021 games
drwxr-xr-x. 2 root root 6 Jun 22 2021 include
drwxr-xr-x. 2 root root 6 Jun 22 2021 lib
drwxr-xr-x. 3 root root 17 Jul 11 21:05 lib64
drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec
lrwxrwxrwx. 1 mysql mysql 35 Jul 27 01:59 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 9 mysql mysql 129 Jul 27 01:57 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Jun 22 2021 sbin
drwxr-xr-x. 5 root root 49 Jul 11 21:05 share
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
[root@linux-1 local]#
[root@linux-1 ~]# cd /usr/local/mysql //进入mysql目录
[root@linux-1 mysql]# ls
LICENSE README bin docs include lib man share support-files
[root@linux-1 mysql]# cd bin/
[root@linux-1 bin]# pwd //打印MySQL的工作路径。
/usr/local/mysql/bin
[root@linux-1 bin]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh //配置环境变量
[root@linux-1 bin]# source /etc/pro
profile profile.d/ protocols
[root@linux-1 bin]# source /etc/profile.d/mysql.sh
[root@linux-1 bin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
[root@linux-1 mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
'/usr/include/mysql' -> '/usr/local/mysql/include/' #创建软连接到mysql目录下
[root@linux-1 mysql]# vim /etc/ld.so.conf.d/mysq.conf
-bash: vim: command not found
[root@linux-1 mysql]# vi /etc/ld.so.conf.d/mysq.conf
[root@linux-1 mysql]# cat /etc/ld.so.conf.d/mysq.conf //头文件
/usr/local/mysql/lib/
[root@linux-1 mysql]# ldconfig //重新加载配置文件。
[root@linux-1 mysql]# vi /etc/man_db.conf //写man文件
#MANDATORY_MANPATH /usr/src/pvm3/man
#
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/mysql/man
#---------------------------------------------------------
mysql配置
[root@linux-1 ~]# mkdir /opt/data //创建数据目录
[root@linux-1 ~]# ll /opt/data/
total 0
[root@linux-1 ~]# chown -R mysql.mysql /opt/data/ //该数据目录的权限为mysql
[root@linux-1 ~]# mysqld --initialize --user mysql --datadir /opt/data/ //初始化数据库,且最后一行会随机生成密码。
2022-07-27T06:21:06.249739Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-27T06:21:06.437040Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-27T06:21:06.464566Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-27T06:21:06.470793Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 4c3bca78-0d74-11ed-8bde-000c29856844.
2022-07-27T06:21:06.471563Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-27T06:21:06.696390Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-27T06:21:06.696423Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-27T06:21:06.696780Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-27T06:21:06.713850Z 1 [Note] A temporary password is generated for root@localhost: lG1T#brcf-_o //这里是数据库的新密码。
[root@linux-1 ~]#
[root@linux-1 ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql #这里是存放路径,可以更改
datadir = /opt/data
socket = /tmp/mysql.sock #套接字一般都会放在/tmp/目录下
port = 3306 #服务端口号可以更改
pid-file = /opt/data/mysql.pid #进程的文件
user = mysql #这里是用户
skip-name-resolve #跳过名称解析
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[root@linux-1 ~]#
用service启动的方法
[root@linux-1 ~]# cp -a /usr/local/mysql-5.7.38-linux-glibc2.12-x86_64/support-files/mysql.server /etc/init.d/mysqld
[root@linux-1 ~]# vi /etc/init.d/mysqld
[root@linux-1 ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/linux-1.err'.
SUCCESS!
[root@linux-1 ~]#
[root@linux-1 ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@linux-1 ~]#
[root@linux-1 ~]# ls
anaconda-ks.cfg
mysql密码破解
1.修改/etc/my.cnf配置文件
vim/etc/my.cnf
skip-grant-tables
2.重启mysql服务
3.mysql 登入修改密码
update user set authentication_string = password(xxxxx) where User = ‘root’ and Host= ‘localhost’;
4、编辑/etc/my.cnf配置文,删除skip-grant-tables,然后重启启动mysql服务
5、使用新密码登入mysql测试
//修改配置文件
[root@localhost ~]# vim /etc/my.cnf
skip-grant-tables //跳过所有授权表
//重启mysql服务
[root@localhost ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
//登入mysql修改密码
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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 |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> use mysql; //进入mysql
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> show tables; //查看当前表
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
······
mysql> select * from user\G; //以列的方式查看user表
*************************** 1. row ***************************
Host: localhost
User: root
Select_priv: Y
······
plugin: mysql_native_password
authentication_string: *9C6D2CC3C59040B6A178DE675CC988EA81F0C84C //找到加密后的
密码字段
password_expired: N
password_last_changed: 2022-07-27 11:27:54
password_lifetime: NULL
account_locked: N
mysql> update user set authentication_string = password('654321') where User =
'root' and Host = 'localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> exit
Bye
//编辑配置文件
[root@localhost ~]# vim /etc/my.cnf
skip-grant-tables //删除此行
//重启服务
[root@localhost ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@localhost ~]#
//使用新密码登录测试
[root@localhost ~]# mysql -uroot -p654321
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 2
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates
多表联合查询
什么是多表联合查询
前面所讲的查询语句都是针对一个表的,但是在关系型数据库中,表与表之间是有联系的,所以在实际应用中,经常使用多表查询。多表查询就是同时查询两个或两个以上的表。
在 MySQL 中,多表查询主要有交叉连接、内连接、外连接、分组查询与子查询等5种。
交叉连接(CROSS JOIN)
交叉连接(CROSS JOIN):有两种,显式的和隐式的2种,一般用来返回连接表的笛卡尔积。
笛卡尔积(Cartesian product)是指两个集合 X 和 Y 的乘积。
交叉连接的语法格式如下:
SELECT <字段名> FROM <表1> CROSS JOIN <表2> [WHERE子句];
或
SELECT <字段名> FROM <表1>, <表2> [WHERE子句];
语法说明如下:
- 字段名:需要查询的字段名称。
- <表1><表2>:需要交叉连接的表名。
- WHERE 子句:用来设置交叉连接的查询条件。
注意:多个表交叉连接时,在 FROM 后连续使用 CROSS JOIN 或,即可。以上两种语法的返回结果是相同的,但是第一种语法才是官方建议的标准写法。
当连接的表之间没有关系时,我们会省略掉 WHERE 子句,这时返回结果就是两个表的笛卡尔积,返回结果数量就是两个表的数据行相乘。需要注意的是,如果每个表有 1000 行,那么返回结果的数量就有 1000×1000 = 1000000 行,数据量是非常巨大的。
交叉连接可以查询两个或两个以上的表,为了更好的理解,我们就讲解两个表的交叉连接查询。
MariaDB [(none)]> create database runtime;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> show database
-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'database' at line 1
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| runtime |
+--------------------+
4 rows in set (0.008 sec)
MariaDB [(none)]> use runtime;
Database changed
MariaDB [runtime]>
MariaDB [runtime]> set character_set_database = 'utf8';
Query OK, 0 rows affected (0.000 sec)
MariaDB [runtime]> show variables like 'character%'
-> ;
+--------------------------+------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mariadb/charsets/ |
+--------------------------+------------------------------+
8 rows in set (0.001 sec)
MariaDB [runtime]> set character_set_client = 'utf8';
Query OK, 0 rows affected (0.000 sec)
MariaDB [runtime]> set character_set_latin1 = 'utf8';
ERROR 1193 (HY000): Unknown system variable 'character_set_latin1'
MariaDB [runtime]> set character_set_connection = 'utf8';
Query OK, 0 rows affected (0.000 sec)
MariaDB [runtime]> set character_set_filesystem = 'utf8';
Query OK, 0 rows affected (0.000 sec)
MariaDB [runtime]> set character_set_results = 'utf8';
Query OK, 0 rows affected (0.000 sec)
MariaDB [runtime]> set character_set_server = 'utf8';
Query OK, 0 rows affected (0.000 sec)
MariaDB [runtime]> show variables like 'character%' #全改成了utf8格式
-> ;
+--------------------------+------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | utf8 |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mariadb/charsets/ |
+--------------------------+------------------------------+
8 rows in set (0.001 sec)
MariaDB [runtime]>
MariaDB [runtime]> desc tb_students_info; #查看表的结构。
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| sex | varchar(4) | YES | | NULL | |
| height | float | YES | | NULL | |
| course_id | int(11) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
6 rows in set (0.001 sec)
#再刚刚创建的表里面插入数据。
MariaDB [runtime]>insert tb_students_info(name,age,sex,height,course_id)
-> values('Dany',25,'w',160,1),('Green',23,'w',158,2),('Henry
'> ',23,'m',185,1),('Jane',22,'w',12,3),('Jim',24,'m',175,2),
-> ('john',21,'m',172,4),('Lilyi',22,'m',165,4),('Suan',23,'w',170,5),
-> ('Thomas',22,'w',178,5),( 'Tom',23,'w',165,5);
Query OK, 10 rows affected (0.001 sec)
Records: 10 Duplicates: 0 Warnings: 0
MariaDB [runtime]> select * from tb_students_info;
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 1 | Dany | 25 | w | 160 | 1 |
| 2 | Green | 23 | w | 158 | 2 |
| 3 | Henry
| 23 | m | 185 | 1 |
| 4 | Jane | 22 | w | 12 | 3 |
| 5 | Jim | 24 | m | 175 | 2 |
| 6 | john | 21 | m | 172 | 4 |
| 7 | Lilyi | 22 | m | 165 | 4 |
| 8 | Suan | 23 | w | 170 | 5 |
| 9 | Thomas | 22 | w | 178 | 5 |
| 10 | Tom | 23 | w | 165 | 5 |
+----+--------+------+------+--------+-----------+
10 rows in set (0.000 sec)
#创建表的结构
MariaDB [runtime]> create table tb_course(id int not null primary key
-> auto_increment , course_name varchar (50));
Query OK, 0 rows affected (0.004 sec)
MariaDB [runtime]> desc tb_course;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| course_name | varchar(50) | YES | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
2 rows in set (0.001 sec)
MariaDB [runtime]> insert tb_course(course_name) values ( 'Java') , ( 'Nysql'), ( 'Python' ) , ( 'Go'), ('C++');
Query OK, 5 rows affected (0.001 sec)
Records: 5 Duplicates: 0 Warnings: 0
#在表里面插入数据
MariaDB [runtime]> select * from tb_course;
+----+-------------+
| id | course_name |
+----+-------------+
| 1 | Java |
| 2 | Nysql |
| 3 | Python |
| 4 | Go |
| 5 | C++ |
+----+-------------+
5 rows in set (0.000 sec)
#查出两张表的笛卡尔集
MariaDB [runtime]>
MariaDB [runtime]> select * from tb_course cross join tb_students_info;
+----+-------------+----+--------+------+------+--------+-----------+
| id | course_name | id | name | age | sex | height | course_id |
+----+-------------+----+--------+------+------+--------+-----------+
| 1 | Java | 1 | Dany | 25 | w | 160 | 1 |
| 2 | Nysql | 1 | Dany | 25 | w | 160 | 1 |
| 3 | Python | 1 | Dany | 25 | w | 160 | 1 |
| 4 | Go | 1 | Dany | 25 | w | 160 | 1 |
| 5 | C++ | 1 | Dany | 25 | w | 160 | 1 |
| 1 | Java | 2 | Green | 23 | w | 158 | 2 |
| 2 | Nysql | 2 | Green | 23 | w | 158 | 2 |
| 3 | Python | 2 | Green | 23 | w | 158 | 2 |
| 4 | Go | 2 | Green | 23 | w | 158 | 2 |
| 5 | C++ | 2 | Green | 23 | w | 158 | 2 |
| 1 | Java | 3 | Henry
| 23 | m | 185 | 1 |
| 2 | Nysql | 3 | Henry
| 23 | m | 185 | 1 |
| 3 | Python | 3 | Henry
| 23 | m | 185 | 1 |
| 4 | Go | 3 | Henry
| 23 | m | 185 | 1 |
| 5 | C++ | 3 | Henry
| 23 | m | 185 | 1 |
| 1 | Java | 4 | Jane | 22 | w | 12 | 3 |
| 2 | Nysql | 4 | Jane | 22 | w | 12 | 3 |
| 3 | Python | 4 | Jane | 22 | w | 12 | 3 |
| 4 | Go | 4 | Jane | 22 | w | 12 | 3 |
| 5 | C++ | 4 | Jane | 22 | w | 12 | 3 |
| 1 | Java | 5 | Jim | 24 | m | 175 | 2 |
| 2 | Nysql | 5 | Jim | 24 | m | 175 | 2 |
| 3 | Python | 5 | Jim | 24 | m | 175 | 2 |
| 4 | Go | 5 | Jim | 24 | m | 175 | 2 |
| 5 | C++ | 5 | Jim | 24 | m | 175 | 2 |
| 1 | Java | 6 | john | 21 | m | 172 | 4 |
| 2 | Nysql | 6 | john | 21 | m | 172 | 4 |
| 3 | Python | 6 | john | 21 | m | 172 | 4 |
| 4 | Go | 6 | john | 21 | m | 172 | 4 |
| 5 | C++ | 6 | john | 21 | m | 172 | 4 |
| 1 | Java | 7 | Lilyi | 22 | m | 165 | 4 |
| 2 | Nysql | 7 | Lilyi | 22 | m | 165 | 4 |
| 3 | Python | 7 | Lilyi | 22 | m | 165 | 4 |
| 4 | Go | 7 | Lilyi | 22 | m | 165 | 4 |
| 5 | C++ | 7 | Lilyi | 22 | m | 165 | 4 |
| 1 | Java | 8 | Suan | 23 | w | 170 | 5 |
| 2 | Nysql | 8 | Suan | 23 | w | 170 | 5 |
| 3 | Python | 8 | Suan | 23 | w | 170 | 5 |
| 4 | Go | 8 | Suan | 23 | w | 170 | 5 |
| 5 | C++ | 8 | Suan | 23 | w | 170 | 5 |
| 1 | Java | 9 | Thomas | 22 | w | 178 | 5 |
| 2 | Nysql | 9 | Thomas | 22 | w | 178 | 5 |
| 3 | Python | 9 | Thomas | 22 | w | 178 | 5 |
| 4 | Go | 9 | Thomas | 22 | w | 178 | 5 |
| 5 | C++ | 9 | Thomas | 22 | w | 178 | 5 |
| 1 | Java | 10 | Tom | 23 | w | 165 | 5 |
| 2 | Nysql | 10 | Tom | 23 | w | 165 | 5 |
| 3 | Python | 10 | Tom | 23 | w | 165 | 5 |
| 4 | Go | 10 | Tom | 23 | w | 165 | 5 |
| 5 | C++ | 10 | Tom | 23 | w | 165 | 5 |
+----+-------------+----+--------+------+------+--------+-----------+
50 rows in set (0.001 sec)
#查找两张表里相同的字段。
MariaDB [runtime]> select * from tb_course cross join tb_students_info where tb_course.id = tb_students_info.course_id;
+----+-------------+----+--------+------+------+--------+-----------+
| id | course_name | id | name | age | sex | height | course_id |
+----+-------------+----+--------+------+------+--------+-----------+
| 1 | Java | 1 | Dany | 25 | w | 160 | 1 |
| 1 | Java | 3 | Henry
| 23 | m | 185 | 1 |
| 2 | Nysql | 2 | Green | 23 | w | 158 | 2 |
| 2 | Nysql | 5 | Jim | 24 | m | 175 | 2 |
| 3 | Python | 4 | Jane | 22 | w | 12 | 3 |
| 4 | Go | 6 | john | 21 | m | 172 | 4 |
| 4 | Go | 7 | Lilyi | 22 | m | 165 | 4 |
| 5 | C++ | 8 | Suan | 23 | w | 170 | 5 |
| 5 | C++ | 9 | Thomas | 22 | w | 178 | 5 |
| 5 | C++ | 10 | Tom | 23 | w | 165 | 5 |
+----+-------------+----+--------+------+------+--------+-----------+
10 rows in set (0.001 sec)
#列出学习对应的学生名单。
MariaDB [runtime]>
MariaDB [runtime]> select tb_course.course_name,tb_students_info.name from tb_course cross join tb_students_info where tb_course.id = tb_students_info.course_id;
+-------------+--------+
| course_name | name |
+-------------+--------+
| Java | Dany |
| Java | Henry
|
| Nysql | Green |
| Nysql | Jim |
| Python | Jane |
| Go | john |
| Go | Lilyi |
| C++ | Suan |
| C++ | Thomas |
| C++ | Tom |
+-------------+--------+
10 rows in set (0.000 sec)
#列出学习java的学生
MariaDB [runtime]>
MariaDB [runtime]> select tb_course.course_name,tb_students_info.name from tb_course cross join tb_students_info where tb_course.id = tb_students_info.course_id and tb_course.course_name = 'JaVa';
+-------------+--------+
| course_name | name |
+-------------+--------+
| Java | Dany |
| Java | Henry
|
+-------------+--------+
2 rows in set (0.001 sec)
MariaDB [runtime]>
#显示学习java课程的平均年龄值,最小年龄值,最大年龄值。
MariaDB [runtime]> select tb_course.course_name,avg(tb_students_info.age) from
-> tb_course cross join tb_students_info where tb_course.id =
-> tb_students_info.course_id and tb_course.course_name = 'Java';
+-------------+---------------------------+
| course_name | avg(tb_students_info.age) |
+-------------+---------------------------+
| Java | 24.0000 |
+-------------+---------------------------+
1 row in set (0.000 sec)
MariaDB [runtime]> select tb_course.course_name,min(tb_students_info.age) from tb_course cross join tb_students_info where tb_course.id = tb_students_info.course_id and tb_course.course_name = 'Java';
+-------------+---------------------------+
| course_name | min(tb_students_info.age) |
+-------------+---------------------------+
| Java | 23 |
+-------------+---------------------------+
1 row in set (0.001 sec)
MariaDB [runtime]> select tb_course.course_name,max(tb_students_info.age) from tb_course cross join tb_students_info where tb_course.id = tb_students_info.course_id and tb_course.course_name = 'Java';
+-------------+---------------------------+
| course_name | max(tb_students_info.age) |
+-------------+---------------------------+
| Java | 25 |
+-------------+---------------------------+
1 row in set (0.000 sec)
MariaDB [runtime]>
23 |
±------------±--------------------------+
1 row in set (0.001 sec)
MariaDB [runtime]> select tb_course.course_name,max(tb_students_info.age) from tb_course cross join tb_students_info where tb_course.id = tb_students_info.course_id and tb_course.course_name = ‘Java’;
±------------±--------------------------+
| course_name | max(tb_students_info.age) |
±------------±--------------------------+
| Java | 25 |
±------------±--------------------------+
1 row in set (0.000 sec)
MariaDB [runtime]>