二进制格式mysql安装与mysql交叉连接
文章目录
- 二进制格式mysql安装
- 安装完成进入数据库
- 破解密码
- 多表联合查询
- 交叉连接(CROSS JOIN)
二进制格式mysql安装
- 前往mysql官网下载二进制安装包https://downloads.mysql.com/archives/community/(注意:选择操作系统时选Linux-Generic)我这里下载的是mysql-8.0.29-linux-glibc2.12-x86_64.tar.xz
//下载二进制包
[root@localhost ~]# cd /usr/src/
[root@192 src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
//创建用户和组
[root@192 src]# useradd -M -r -s /sbin/nologin mysql
[root@192 src]# id mysql
uid=995(mysql) gid=992(mysql) groups=992(mysql)
//解压二进制包
[root@192 src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@192 src]# ls /usr/local/
bin include libexec share
etc lib mysql-5.7.38-linux-glibc2.12-x86_64 src
games lib64 sbin
//创建软连接
[root@192 ~]# cd /usr/local/
[root@192 local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64/'
[root@192 local]# ll
total 0
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 25 20:12 lib64
drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec
lrwxrwxrwx. 1 root root 36 Jul 27 15:39 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Jul 27 15:38 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 25 20:12 share
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
//修改目录/usr/local/mysql的属主属组
[root@192 local]# chown -R mysql.mysql mysql*
[root@192 local]# ll
total 0
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 25 20:12 lib64
drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec
lrwxrwxrwx. 1 mysql mysql 36 Jul 27 15:39 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 Jul 27 15:38 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 25 20:12 share
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
//添加环境变量
[root@192 ~]# cd /usr/local/mysql/bin/
[root@192 bin]# pwd
/usr/local/mysql/bin
[root@192 bin]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@192 bin]# source /etc/profile.d/mysql.sh
[root@192 bin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
//创建头文件软连接到/usr/incloud/mysql
[root@192 mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
'/usr/include/mysql' -> '/usr/local/mysql/include/'
//配置lib
[root@192 mysql]# vim /etc/ld.so.conf.d/mysql.conf
[root@192 mysql]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/
[root@192 mysql]# ldconfig
//配置man
[root@192 mysql]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/mysql/man
//建立数据存放目录
[root@192 ~]# mkdir /opt/data
[root@192 ~]# chown -R mysql.mysql /opt/data/
//初始化数据库,请注意,这个命令的最后会生成一个临时密码
[root@192 ~]# mysqld --initialize --user mysql --datadir /opt/data/
2022-07-27T07:52:57.535818Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-27T07:52:57.720839Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-27T07:52:57.765114Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-27T07:52:57.830226Z 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: 214280fd-0d81-11ed-9a09-000c29a9872e.
2022-07-27T07:52:57.831162Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-27T07:52:58.048317Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-27T07:52:58.048356Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-27T07:52:58.048747Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-27T07:52:58.138803Z 1 [Note] A temporary password is generated for root@localhost: hAxQ36EpNK<j
//生成配置文件
[root@192 ~]# vim /etc/my.cnf
[root@192 ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
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@192 ~]#
//配置mysql启动配置
[root@192 ~]# cp /usr/lib/systemd/system/sshd.service .
[root@192 ~]# mv sshd.service mysqld.service
[root@192 ~]# vim mysqld.service
[root@192 ~]# cat mysqld.service
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@192 ~]# mv mysqld.service /usr/lib/systemd/system
[root@192 ~]# systemctl daemon-reload
[root@192 ~]# systemctl start mysqld
[root@192 ~]# systemctl status mysqld.service
● mysqld.service - mysql server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disab>
Active: active (running) since Wed 2022-07-27 16:03:17 CST; 3>
Process: 2705 ExecStart=/usr/local/mysql/support-files/mysql.s>
Main PID: 2718 (mysqld_safe)
Tasks: 28 (limit: 11217)
Memory: 175.9M
CGroup: /system.slice/mysqld.service
├─2718 /bin/sh /usr/local/mysql/bin/mysqld_safe --dat>
└─2918 /usr/local/mysql/bin/mysqld --basedir=/usr/loc>
Jul 27 16:03:16 192.168.159.162 systemd[1]: Starting mysql serve>
Jul 27 16:03:16 192.168.159.162 mysql.server[2705]: Starting MyS>
Jul 27 16:03:17 192.168.159.162 mysql.server[2705]: SUCCESS!
Jul 27 16:03:17 192.168.159.162 systemd[1]: Started mysql server>
lines 1-15/15 (END)
[root@192 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
安装完成进入数据库
- 输入初始化时给的临时密码
[root@192 ~]# mysql -uroot -p'hAxQ36EpNK<j'
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
//这没有进入是因为缺少一个包,不要慌下载一个就好了
[root@192 ~]# yum whatprovides libncurses.so.5
Last metadata expiration check: 0:11:11 ago on Wed 27 Jul 2022 03:58:38 PM CST.
ncurses-compat-libs-6.1-9.20180224.el8.i686 : Ncurses
...: compatibility libraries
Repo : base
Matched from:
Provide : libncurses.so.5
[root@192 ~]# yum -y install ncurses-compat-libs
//再次进入成功
[root@192 ~]# mysql -uroot -p'hAxQ36EpNK<j'
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
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>
//修改密码
mysql> set password = password('123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
[root@192 ~]# mysql -uroot -p123
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 3
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>
破解密码
[root@192 ~]# vim /etc/my.cnf
skip-grant-tables //手动添加这一行
//免密进入数据库修改密码
[root@192 ~]# 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> 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> update user set //修改root密码authentication_string=password('456') where user='root';
;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
//编辑配置文件将skip-grant-tables参数注释或删除
[root@192 ~]# vim /etc/my.cnf
#skip-grant-tables
//登录测试
[root@192 ~]# mysql -uroot -p456
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 3
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>
多表联合查询
- 前面所讲的查询语句都是针对一个表的,但是在关系型数据库中,表与表之间是有联系的,所以在实际应用中,经常使用多表查询。多表查询就是同时查询两个或两个以上的表。
在 MySQL 中,多表查询主要有交叉连接、内连接、外连接、分组查询与子查询等5种。
交叉连接(CROSS JOIN)
- 交叉连接(CROSS JOIN):有两种,显式的和隐式的2种,一般用来返回连接表的笛卡尔积。
- 笛卡尔积(Cartesian product)是指两个集合 X 和 Y 的乘积。
多表查询遵循的算法就是以上提到的笛卡尔积,表与表之间的连接可以看成是在做乘法运算。在实际应用中,应避免使用笛卡尔积,因为笛卡尔积中容易存在大量的不合理数据,简单来说就是容易导致查询结果重复、混乱。
语法说明如下:
- 字段名:需要查询的字段名称。
- <表1><表2>:需要交叉连接的表名。
- WHERE 子句:用来设置交叉连接的查询条件。
注意:多个表交叉连接时,在 FROM 后连续使用 CROSS JOIN 或,即可。以上两种语法的返回结果是相同的,但是第一种语法才是官方建议的标准写法。
当连接的表之间没有关系时,我们会省略掉 WHERE 子句,这时返回结果就是两个表的笛卡尔积,返回结果数量就是两个表的数据行相乘。需要注意的是,如果每个表有 1000 行,那么返回结果的数量就有 1000×1000 = 1000000 行,数据量是非常巨大的。
交叉连接可以查询两个或两个以上的表,为了更好的理解,我们就讲解两个表的交叉连接查询。
例 1
查询学生信息表和科目信息表,并得到一个笛卡尔积。
为了方便观察学生信息表和科目表交叉连接后的运行结果,我们先分别查询出这两个表的数据,再进行交叉连接查询。
//创建tb_students_info表并插入以下数据
//先创建一个数据库
mysql> create database runtime;
Query OK, 1 row affected (0.00 sec)
//进入数据库
mysql> use runtime;
Database changed
//创建表并设置字段类型
mysql> create table tb_students_info(id int not null primary keyauto_increment,name varchar(50),age tinyint,sex varchar(4),height float,course_id int) default charset=utf8;
Query OK, 0 rows affected (0.00 sec)
//查看表结构
mysql> 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.00 sec)
//插入字段
mysql> insert into tb_students_info (name,age,sex,height,course_id) values('Dany',25,'男',160,1),('Green',23,'男',158,2),('Henry',223,'女',185,1),('Jane',22,'男',162,3),('Jim',24,'女',175,2),('Johnn',21,'女',172,4),('Lily',22,'男',165,4),('Susan',23,'男',170,5),(('Thomas',22,'女',178,5),('Tom',23,'女',165,5);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
//显示表内容
mysql> select * from tb_students_info;
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 1 | Dany | 25 | 男 | 160 | 1 |
| 2 | Green | 23 | 男 | 158 | 2 |
| 3 | Henry | 23 | 女 | 185 | 1 |
| 4 | Jane | 22 | 男 | 162 | 3 |
| 5 | Jim | 24 | 女 | 175 | 2 |
| 6 | John | 21 | 女 | 172 | 4 |
| 7 | Lily | 22 | 男 | 165 | 4 |
| 8 | Susan | 23 | 男 | 170 | 5 |
| 9 | Thomas | 22 | 女 | 178 | 5 |
| 10 | Tom | 23 | 女 | 165 | 5 |
+----+--------+------+------+--------+-----------+
10 rows in set (0.00 sec)
//再创建一个tb_course表如下
//创建表字段类型
mysql> create table tb_course(id int not null primary key auto_increment,course_name varchar(50));
Query OK, 0 rows affected (0.00 sec)
//表结构
mysql> 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.00 sec)
//插入数据
mysql> insert into tb_course(course_name) values('Java'),('MySQL')),('Python'),('Go'),('C++');
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
//查看表数据
mysql> select * from tb_course;
+----+-------------+
| id | course_name |
+----+-------------+
| 1 | Java |
| 2 | MySQL |
| 3 | Python |
| 4 | Go |
| 5 | C++ |
+----+-------------+
5 rows in set (0.00 sec)
//使用 CROSS JOIN 查询出两张表中的笛卡尔积,SQL 语句和运行结果如下:
mysql> select * from tb_course cross join tb_students_info;
+----+-------------+----+--------+------+------+--------+-----------+
| id | course_name | id | name | age | sex | height | course_id |
+----+-------------+----+--------+------+------+--------+-----------+
| 1 | Java | 1 | Dany | 25 | 男 | 160 | 1 |
| 2 | MySQL | 1 | Dany | 25 | 男 | 160 | 1 |
| 3 | Python | 1 | Dany | 25 | 男 | 160 | 1 |
| 4 | Go | 1 | Dany | 25 | 男 | 160 | 1 |
| 5 | C++ | 1 | Dany | 25 | 男 | 160 | 1 |
| 1 | Java | 2 | Green | 23 | 男 | 158 | 2 |
| 2 | MySQL | 2 | Green | 23 | 男 | 158 | 2 |
| 3 | Python | 2 | Green | 23 | 男 | 158 | 2 |
| 4 | Go | 2 | Green | 23 | 男 | 158 | 2 |
| 5 | C++ | 2 | Green | 23 | 男 | 158 | 2 |
| 1 | Java | 3 | Henry | 23 | 女 | 185 | 1 |
| 2 | MySQL | 3 | Henry | 23 | 女 | 185 | 1 |
| 3 | Python | 3 | Henry | 23 | 女 | 185 | 1 |
| 4 | Go | 3 | Henry | 23 | 女 | 185 | 1 |
| 5 | C++ | 3 | Henry | 23 | 女 | 185 | 1 |
| 1 | Java | 4 | Jane | 22 | 男 | 162 | 3 |
| 2 | MySQL | 4 | Jane | 22 | 男 | 162 | 3 |
| 3 | Python | 4 | Jane | 22 | 男 | 162 | 3 |
| 4 | Go | 4 | Jane | 22 | 男 | 162 | 3 |
| 5 | C++ | 4 | Jane | 22 | 男 | 162 | 3 |
| 1 | Java | 5 | Jim | 24 | 女 | 175 | 2 |
| 2 | MySQL | 5 | Jim | 24 | 女 | 175 | 2 |
| 3 | Python | 5 | Jim | 24 | 女 | 175 | 2 |
| 4 | Go | 5 | Jim | 24 | 女 | 175 | 2 |
| 5 | C++ | 5 | Jim | 24 | 女 | 175 | 2 |
| 1 | Java | 6 | John | 21 | 女 | 172 | 4 |
| 2 | MySQL | 6 | John | 21 | 女 | 172 | 4 |
| 3 | Python | 6 | John | 21 | 女 | 172 | 4 |
| 4 | Go | 6 | John | 21 | 女 | 172 | 4 |
| 5 | C++ | 6 | John | 21 | 女 | 172 | 4 |
| 1 | Java | 7 | Lily | 22 | 男 | 165 | 4 |
| 2 | MySQL | 7 | Lily | 22 | 男 | 165 | 4 |
| 3 | Python | 7 | Lily | 22 | 男 | 165 | 4 |
| 4 | Go | 7 | Lily | 22 | 男 | 165 | 4 |
| 5 | C++ | 7 | Lily | 22 | 男 | 165 | 4 |
| 1 | Java | 8 | Susan | 23 | 男 | 170 | 5 |
| 2 | MySQL | 8 | Susan | 23 | 男 | 170 | 5 |
| 3 | Python | 8 | Susan | 23 | 男 | 170 | 5 |
| 4 | Go | 8 | Susan | 23 | 男 | 170 | 5 |
| 5 | C++ | 8 | Susan | 23 | 男 | 170 | 5 |
| 1 | Java | 9 | Thomas | 22 | 女 | 178 | 5 |
| 2 | MySQL | 9 | Thomas | 22 | 女 | 178 | 5 |
| 3 | Python | 9 | Thomas | 22 | 女 | 178 | 5 |
| 4 | Go | 9 | Thomas | 22 | 女 | 178 | 5 |
| 5 | C++ | 9 | Thomas | 22 | 女 | 178 | 5 |
| 1 | Java | 10 | Tom | 23 | 女 | 165 | 5 |
| 2 | MySQL | 10 | Tom | 23 | 女 | 165 | 5 |
| 3 | Python | 10 | Tom | 23 | 女 | 165 | 5 |
| 4 | Go | 10 | Tom | 23 | 女 | 165 | 5 |
| 5 | C++ | 10 | Tom | 23 | 女 | 165 | 5 |
+----+-------------+----+--------+------+------+--------+-----------+
50 rows in set (0.00 sec)
//查询 tb_course 表中的 id 字段和 tb_students_info 表中的 course_id 字段相等的内容, SQL 语句和运行结果如下:
mysql> select * from tb_course cross join tb_students_info wheretb_students_info.course_id = tb_course.id;
+----+-------------+----+--------+------+------+--------+-----------+
| id | course_name | id | name | age | sex | height | course_id |
+----+-------------+----+--------+------+------+--------+-----------+
| 1 | Java | 1 | Dany | 25 | 男 | 160 | 1 |
| 2 | MySQL | 2 | Green | 23 | 男 | 158 | 2 |
| 1 | Java | 3 | Henry | 23 | 女 | 185 | 1 |
| 3 | Python | 4 | Jane | 22 | 男 | 162 | 3 |
| 2 | MySQL | 5 | Jim | 24 | 女 | 175 | 2 |
| 4 | Go | 6 | John | 21 | 女 | 172 | 4 |
| 4 | Go | 7 | Lily | 22 | 男 | 165 | 4 |
| 5 | C++ | 8 | Susan | 23 | 男 | 170 | 5 |
| 5 | C++ | 9 | Thomas | 22 | 女 | 178 | 5 |
| 5 | C++ | 10 | Tom | 23 | 女 | 165 | 5 |
+----+-------------+----+--------+------+------+--------+-----------+
10 rows in set (0.00 sec)
//只查询课程名和对应的人名
mysql> select tb_course.course_name,tb_students_info.name from tb_course cross join tb_students_info where tb_students_info.course_id = tb_course.id;
+-------------+--------+
| course_name | name |
+-------------+--------+
| Java | Dany |
| MySQL | Green |
| Java | Henry |
| Python | Jane |
| MySQL | Jim |
| Go | John |
| Go | Lily |
| C++ | Susan |
| C++ | Thomas |
| C++ | Tom |
+-------------+--------+
10 rows in set (0.00 sec)
//查询学习java的人
mysql> select tb_course.course_name,tb_students_info.name from tb_course cross join tb_students_info where tb_students_info.course_id = tb_course.id and tb_course.course_name = 'Java';
+-------------+-------+
| course_name | name |
+-------------+-------+
| Java | Dany |
| Java | Henry |
+-------------+-------+
2 rows in set (0.00 sec)
//查看学习java的平均年龄
mysql> select tb_course.course_name,avg(tb_students_info.age) from tb_course crross join tb_students_info where tb_students_info.course_id = tb_course.id and tb_course.course_name = 'Java';
+-------------+---------------------------+
| course_name | avg(tb_students_info.age) |
+-------------+---------------------------+
| Java | 24.0000 |
+-------------+---------------------------+
1 row in set (0.00 sec)
-------+
2 rows in set (0.00 sec)
//查看学习java的平均年龄
mysql> select tb_course.course_name,avg(tb_students_info.age) from tb_course crross join tb_students_info where tb_students_info.course_id = tb_course.id and tb_course.course_name = ‘Java’;
±------------±--------------------------+
| course_name | avg(tb_students_info.age) |
±------------±--------------------------+
| Java | 24.0000 |
±------------±--------------------------+
1 row in set (0.00 sec)