mysql进阶

打印 上一主题 下一主题

主题 933|帖子 933|积分 2799

mysql进阶


目录

1.二进制格式mysql安装

下载二进制格式的mysql软件包
  1. [root@localhost ~]# cd /usr/src/
  2. [root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
  3. --2022-07-27 15:52:00--  https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
  4. Resolving cdn.mysql.com (cdn.mysql.com)... 23.33.16.230
  5. Connecting to cdn.mysql.com (cdn.mysql.com)|23.33.16.230|:443... connected.
  6. HTTP request sent, awaiting response... 200 OK
  7. Length: 674830866 (644M) [application/x-tar-gz]
  8. Saving to: 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz'
  9. mysql-5.7.38-linux- 100%[=================>] 643.57M  1.43MB/s    in 4m 25s  
  10. 2022-07-27 15:56:27 (2.42 MB/s) - 'mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz' saved [674830866/674830866]
复制代码
创建用户和组
  1. [root@localhost src]# useradd -M -r -s /sbin/nologin mysql
  2. [root@localhost src]# id mysql
  3. uid=995(mysql) gid=992(mysql) groups=992(mysql)
复制代码
解压软件到/usr/local/
  1. [root@localhost src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
  2. [root@localhost src]# ls /usr/local/
  3. bin  games    lib    libexec                              sbin   src
  4. etc  include  lib64  mysql-5.7.38-linux-glibc2.12-x86_64  share
  5. [root@localhost src]# cd /usr/local/
  6. [root@localhost local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
  7. 'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64'
  8. [root@localhost local]# ll
  9. total 0
  10. drwxr-xr-x. 2 root root   6 Jun 22  2021 bin
  11. drwxr-xr-x. 2 root root   6 Jun 22  2021 etc
  12. drwxr-xr-x. 2 root root   6 Jun 22  2021 games
  13. drwxr-xr-x. 2 root root   6 Jun 22  2021 include
  14. drwxr-xr-x. 2 root root   6 Jun 22  2021 lib
  15. drwxr-xr-x. 3 root root  17 Jul 12 21:45 lib64
  16. drwxr-xr-x. 2 root root   6 Jun 22  2021 libexec
  17. lrwxrwxrwx. 1 root root  35 Jul 27 16:06 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64
  18. drwxr-xr-x. 9 root root 129 Jul 27 16:04 mysql-5.7.38-linux-glibc2.12-x86_64
  19. drwxr-xr-x. 2 root root   6 Jun 22  2021 sbin
  20. drwxr-xr-x. 5 root root  49 Jul 12 21:45 share
  21. drwxr-xr-x. 2 root root   6 Jun 22  2021 src
复制代码
修改目录/usr/local/mysql的属主属组
  1. [root@localhost local]# chown -R mysql.mysql mysql*
  2. [root@localhost local]# ll
  3. total 0
  4. drwxr-xr-x. 2 root  root    6 Jun 22  2021 bin
  5. drwxr-xr-x. 2 root  root    6 Jun 22  2021 etc
  6. drwxr-xr-x. 2 root  root    6 Jun 22  2021 games
  7. drwxr-xr-x. 2 root  root    6 Jun 22  2021 include
  8. drwxr-xr-x. 2 root  root    6 Jun 22  2021 lib
  9. drwxr-xr-x. 3 root  root   17 Jul 12 21:45 lib64
  10. drwxr-xr-x. 2 root  root    6 Jun 22  2021 libexec
  11. lrwxrwxrwx. 1 mysql mysql  35 Jul 27 16:06 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64
  12. drwxr-xr-x. 9 mysql mysql 129 Jul 27 16:04 mysql-5.7.38-linux-glibc2.12-x86_64
  13. drwxr-xr-x. 2 root  root    6 Jun 22  2021 sbin
  14. drwxr-xr-x. 5 root  root   49 Jul 12 21:45 share
  15. drwxr-xr-x. 2 root  root    6 Jun 22  2021 src
复制代码
添加环境变量
  1. [root@localhost ~]# cd /usr/local/mysql
  2. [root@localhost mysql]# ls
  3. LICENSE  README  bin  docs  include  lib  man  share  support-files
  4. [root@localhost mysql]# cd bin/
  5. [root@localhost bin]# pwd
  6. /usr/local/mysql/bin
  7. [root@localhost ~]# cd /usr/local/mysql
  8. [root@localhost mysql]# ls
  9. LICENSE  README  bin  docs  include  lib  man  share  support-files
  10. [root@localhost mysql]# cd bin/
  11. [root@localhost bin]# pwd
  12. /usr/local/mysql/bin
  13. [root@localhost bin]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
  14. [root@localhost bin]# source /etc/profile.d/mysql.sh
  15. [root@localhost bin]# echo $PATH
  16. /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
复制代码
创建头文件
  1. [root@localhost bin]# cd ..
  2. [root@localhost mysql]# ls
  3. LICENSE  README  bin  docs  include  lib  man  share  support-files
  4. [root@localhost mysql]# ln -sv /usr/local/mysql
  5. mysql/                               mysql-5.7.38-linux-glibc2.12-x86_64/
  6. [root@localhost mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
  7. '/usr/include/mysql' -> '/usr/local/mysql/include/'
  8. [root@localhost mysql]# vim /etc/ld.so.conf.d/mysql.conf
  9. [root@localhost mysql]# cat /etc/ld.so.conf.d/mysql.conf
  10. /usr/local/mysql/lib/
  11. [root@localhost mysql]# ldconfig
  12. [root@localhost mysql]# vim /etc/man_db.conf
  13. #MANDATORY_MANPATH                      /usr/src/pvm3/man
  14. #
  15. MANDATORY_MANPATH                       /usr/man
  16. MANDATORY_MANPATH                       /usr/share/man
  17. MANDATORY_MANPATH                       /usr/local/share/man
  18. MANDATORY_MANPATH                       /usr/local/mysql/man        //添加一句
复制代码
建立数据存放目录
  1. [root@localhost ~]# mkdir /opt/data
  2. [root@localhost ~]# ll /opt/data/
  3. total 0
  4. [root@localhost ~]# chown -R mysql.mysql /opt/data/
复制代码
初始化数据库
  1. [root@localhost ~]# mysqld --initialze --user mysql --datadir /opt/data/
  2. 2022-07-27T08:28:23.607621Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
  3. 2022-07-27T08:28:23.804276Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
  4. 2022-07-27T08:28:23.804304Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
  5. 2022-07-27T08:28:23.804658Z 0 [Warning] CA certificate ca.pem is self signed.
  6. 2022-07-27T08:28:23.844366Z 1 [Note] A temporary password is generated for root@localhost: RIa<P+tD%6qV               
  7. //这个命令的最后会生成一个临时密码,此处密码是RIa<P+tD%6qV
  8. //再次注意,这个密码是随机的,你的不会跟我一样,一定要记住这个密码,因为一会登录时会用到
复制代码
2.mysql配置文件

mysql的配置文件为/etc/my.cnf
配置文件查找次序:若在多个配置文件中均有设定,则最后找到的最终生效
  1. [root@localhost ~]# vim /etc/my.cnf
  2. [root@localhost ~]# cat /etc/my.cnf
  3. [mysqld]
  4. basedir = /usr/local/mysql
  5. datadir = /opt/data
  6. socket = /tmp/mysql.sock
  7. port = 3306
  8. pid-file = /opt/data/mysql.pid
  9. user = mysql
  10. skip-name-resolve
  11. sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
复制代码
  1. //先找到启动脚本文件位置
  2. [root@localhost ~]# cd /usr/local/mysql
  3. [root@localhost mysql]# ls
  4. LICENSE  README  bin  docs  include  lib  man  share  support-files
  5. [root@localhost mysql]# cd support-files/
  6. [root@localhost support-files]# ls
  7. magic  mysql-log-rotate  mysql.server  mysqld_multi.server
  8. [root@localhost support-files]# file mysql.server
  9. mysql.server: POSIX shell script, ASCII text executable
  10. //在进行启动mysql,发现3306端口已监听
  11. [root@localhost ~]# /usr/local/mysql/support-files/mysql.server start
  12. Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
  13. SUCCESS!
  14. [root@localhost ~]# ss -anlt
  15. State            Recv-Q           Send-Q                       Local Address:Port                       Peer Address:Port           Process           
  16. LISTEN           0                128                                0.0.0.0:22                              0.0.0.0:*                                
  17. LISTEN           0                80                                       *:3306                                  *:*                                
  18. LISTEN           0                128                                   [::]:22                                 [::]:*                           
  19. //将脚本加入到系统里去   //使用service控制MySQL启动或关闭  
  20. [root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
  21. [root@localhost ~]# vim /etc/init.d/mysqld
  22. basedir=/usr/local/mysql/                //添加安装位置
  23. datadir=/opt/data/                                //添加数据库存放位置
  24. [root@localhost ~]# service mysqld start
  25. Starting MySQL. SUCCESS!
  26. [root@localhost ~]# ss -anlt
  27. State            Recv-Q           Send-Q                       Local Address:Port                       Peer Address:Port           Process           
  28. LISTEN           0                128                                0.0.0.0:22                              0.0.0.0:*                                
  29. LISTEN           0                80                                       *:3306                                  *:*                                
  30. LISTEN           0                128                                   [::]:22                                 [::]:*                                
  31. [root@localhost ~]# service mysqld stop
  32. Shutting down MySQL.^[[A^[[A. SUCCESS!
  33. [root@localhost ~]# ss -anlt
  34. State            Recv-Q           Send-Q                       Local Address:Port                       Peer Address:Port           Process           
  35. LISTEN           0                128                                0.0.0.0:22                              0.0.0.0:*                                
  36. LISTEN           0                128                                   [::]:22                                 [::]:*                                
  37. //配置文件被删除后使用不了
  38. [root@localhost ~]# rm -rf /etc/init.d/mysqld
  39. [root@localhost ~]# service mysqld start
  40. Redirecting to /bin/systemctl start mysqld.service
  41. Failed to start mysqld.service: Unit mysqld.service not found.
  42. //使用systemctl管理mysql
  43. [root@localhost ~]# cp /usr/lib/systemd/system/sshd.service .
  44. [root@localhost ~]# ls
  45. anaconda-ks.cfg  passwd  sshd.service
  46. [root@localhost ~]# mv sshd.service mysql.service
  47. [root@localhost ~]# ls
  48. anaconda-ks.cfg  mysql.service  passwd
  49. [root@localhost ~]# vim mysql.service
  50. [root@localhost ~]# cat mysql.service
  51. [Unit]
  52. Description=mysql server daemon
  53. After=network.target sshd-keygen.target
  54. [Service]
  55. Type=forking
  56. ExecStart=/usr/local/mysql/support-files/mysql.server start
  57. ExecStop=/usr/local/mysql/support-files/mysql.server stop
  58. ExecReload=/bin/kill -HUP $MAINPID
  59. [Install]
  60. WantedBy=multi-user.target
  61. [root@localhost ~]# mv mysql.service /usr/lib/systemd/system/
  62. [root@localhost ~]# ls
  63. anaconda-ks.cfg  passwd
  64. [root@localhost ~]# systemctl daemon-reload
  65. [root@localhost ~]# systemctl start mysqld
  66. Failed to start mysqld.service: Unit mysqld.service not found.        //启动报错需要关闭防火墙和selinux
  67. [root@localhost ~]# systemctl stop firewalld
  68. [root@localhost ~]# systemctl disable firewalld
  69. Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
  70. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
  71. [root@localhost ~]# setenforce 0
  72. [root@localhost ~]# vim /etc/selinux/config
  73. SELINUX=disabled        //修改
  74. [root@localhost ~]# systemctl start mysql
  75. [root@localhost ~]# ss -anlt
  76. State            Recv-Q           Send-Q                       Local Address:Port                       Peer Address:Port           Process           
  77. LISTEN           0                128                                0.0.0.0:22                              0.0.0.0:*                                
  78. LISTEN           0                80                                       *:3306                                  *:*                                
  79. LISTEN           0                128                                   [::]:22                                 [::]:*                                
复制代码
mysql常用配置文件参数:
参数说明port = 3306设置监听端口socket = /tmp/mysql.sock指定套接字文件位置basedir = /usr/local/mysql指定MySQL的安装路径datadir = /data/mysql指定MySQL的数据存放路径pid-file = /data/mysql/mysql.pid指定进程ID文件存放路径user = mysql指定MySQL以什么用户的身份提供服务skip-name-resolve禁止MySQL对外部连接进行DNS解析 使用这一选项可以消除MySQL进行DNS解析的时间。 若开启该选项,则所有远程主机连接授权都要使用IP地址方 式否则MySQL将无法正常处理连接请求2.1 数据库密码破解

数据库密码破解:
1、修改/etc/my.cnf配置文件
vim /etc/my.cnf
添加skip-grant-tables
2、重启mysql服务
3、mysql 登入修改密码
update user set authentication_string = password('654321') where User = 'root' and Host = 'localhost';
4、编辑/etc/my.cnf配置文,删除skip-grant-tables,然后重启启动mysql服务
5、使用新密码登入mysql测试
  1. [root@localhost ~]# ls
  2. anaconda-ks.cfg  passwd
  3. [root@localhost ~]# cat passwd
  4. RIa<P+tD%6qV
  5. [root@localhost ~]# mysql -uroot -p'RIa<P+tD%6qV'        //登录报错显示需要libncurses.so.5
  6. mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
  7. [root@localhost ~]# dnf provides libncurses.so.5        //查看这个在那个软件包里面
  8. Failed to set locale, defaulting to C.UTF-8
  9. Last metadata expiration check: 0:37:59 ago on Wed Jul 27 16:33:32 2022.
  10. ncurses-compat-libs-6.1-9.20180224.el8.i686 : Ncurses compatibility libraries
  11. Repo        : base
  12. Matched from:
  13. Provide    : libncurses.so.5
  14. [root@localhost ~]# dnf -y install ncurses-compat-libs        //下载包
  15. [root@localhost ~]# mysql -uroot -p'RIa<P+tD%6qV'
  16. mysql: [Warning] Using a password on the command line interface can be insecure.
  17. Welcome to the MySQL monitor.  Commands end with ; or \g.
  18. Your MySQL connection id is 2
  19. Server version: 5.7.38
  20. Copyright (c) 2000, 2022, Oracle and/or its affiliates.
  21. Oracle is a registered trademark of Oracle Corporation and/or its
  22. affiliates. Other names may be trademarks of their respective
  23. owners.
  24. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  25. mysql> set password = password('123456');
  26. Query OK, 0 rows affected, 1 warning (0.00 sec)
  27. mysql> exit
  28. Bye
  29. [root@localhost ~]# mysql -uroot -p123456
  30. mysql: [Warning] Using a password on the command line interface can be insecure.
  31. Welcome to the MySQL monitor.  Commands end with ; or \g.
  32. Your MySQL connection id is 3
  33. Server version: 5.7.38 MySQL Community Server (GPL)
  34. Copyright (c) 2000, 2022, Oracle and/or its affiliates.
  35. Oracle is a registered trademark of Oracle Corporation and/or its
  36. affiliates. Other names may be trademarks of their respective
  37. owners.
  38. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  39. mysql>
复制代码
3.多表联合查询

3.1 什么是多表联合查询

前面所讲的查询语句都是针对一个表的,但是在关系型数据库中,表与表之间是有联系的,所以在实际应用中,经常使用多表查询。多表查询就是同时查询两个或两个以上的表。
在 MySQL 中,多表查询主要有交叉连接、内连接、外连接、分组查询与子查询等5种。
3.2 交叉连接(CROSS JOIN)

3.2.1 笛卡尔积

交叉连接(CROSS JOIN):有两种,显式的和隐式的2种,一般用来返回连接表的笛卡尔积。
笛卡尔积(Cartesian product)是指两个集合 X 和 Y 的乘积。
例如,有 A 和 B 两个集合,它们的值如下:
  1. /etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf
复制代码
集合 A×B 和 B×A 的结果集分别表示为:
  1. [root@localhost ~]# vim .my.cnf
  2. [root@localhost ~]# cat .my.cnf
  3. [client]
  4. user=root
  5. password=123456
  6. [root@localhost ~]# mysql -e 'show databases';
  7. +--------------------+
  8. | Database           |
  9. +--------------------+
  10. | information_schema |
  11. | mysql              |
  12. | performance_schema |
  13. | sys                |
  14. +--------------------+
复制代码
以上 A×B 和 B×A 的结果就叫做两个集合的笛卡尔积。
并且,从以上结果我们可以看出:

  • 两个集合相乘,不满足交换率,即 A×B≠B×A。
  • A 集合和 B 集合的笛卡尔积是 A 集合的元素个数 × B 集合的元素个数。
多表查询遵循的算法就是以上提到的笛卡尔积,表与表之间的连接可以看成是在做乘法运算。在实际应用中,应避免使用笛卡尔积,因为笛卡尔积中容易存在大量的不合理数据,简单来说就是容易导致查询结果重复、混乱。
3.2.2 交叉连接

交叉连接的语法格式如下:
  1. [root@localhost ~]# vim /etc/my.cnf
  2. [mysqld]
  3. basedir = /usr/local/mysql
  4. datadir = /opt/data
  5. socket = /tmp/mysql.sock
  6. port = 3306
  7. pid-file = /opt/data/mysql.pid
  8. user = mysql
  9. skip-name-resolve
  10. skip-grant-tables                        //添加一句 表示跳过所有授权表
  11. sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
  12. [root@localhost ~]# systemctl restart mysql
  13. [root@localhost ~]# mysql
  14. Welcome to the MySQL monitor.  Commands end with ; or \g.
  15. Your MySQL connection id is 2
  16. Server version: 5.7.38 MySQL Community Server (GPL)
  17. Copyright (c) 2000, 2022, Oracle and/or its affiliates.
  18. Oracle is a registered trademark of Oracle Corporation and/or its
  19. affiliates. Other names may be trademarks of their respective
  20. owners.
  21. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  22. mysql> set password = password('654321');
  23. ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
  24. mysql> show databases;
  25. +--------------------+
  26. | Database           |
  27. +--------------------+
  28. | information_schema |
  29. | mysql              |
  30. | performance_schema |
  31. | sys                |
  32. +--------------------+
  33. 4 rows in set (0.01 sec)
  34. mysql> use mysql;
  35. Reading table information for completion of table and column names
  36. You can turn off this feature to get a quicker startup with -A
  37. mysql> update user set authentication_string = password('654321') where User = 'root' and Host = 'localhost';
  38. Query OK, 1 row affected, 1 warning (0.00 sec)
  39. Rows matched: 1  Changed: 1  Warnings: 1
  40. //删除添加的那一句
  41. [root@localhost ~]# vim /etc/my.cnf
  42. [root@localhost ~]# cat /etc/my.cnf
  43. [mysqld]
  44. basedir = /usr/local/mysql
  45. datadir = /opt/data
  46. socket = /tmp/mysql.sock
  47. port = 3306
  48. pid-file = /opt/data/mysql.pid
  49. user = mysql
  50. skip-name-resolve
  51. sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
  52. //测试密码是否能登录
  53. [root@localhost ~]# mysql -uroot -p654321
  54. mysql: [Warning] Using a password on the command line interface can be insecure.
  55. Welcome to the MySQL monitor.  Commands end with ; or \g.
  56. Your MySQL connection id is 3
  57. Server version: 5.7.38 MySQL Community Server (GPL)
  58. Copyright (c) 2000, 2022, Oracle and/or its affiliates.
  59. Oracle is a registered trademark of Oracle Corporation and/or its
  60. affiliates. Other names may be trademarks of their respective
  61. owners.
  62. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  63. mysql>
复制代码
  1. A = {1,2}
  2. B = {3,4,5}
复制代码
注意:多个表交叉连接时,在 FROM 后连续使用 CROSS JOIN 或,即可。以上两种语法的返回结果是相同的,但是第一种语法才是官方建议的标准写法。
先创建一个名为tb_student_info的表
  1. A×B={(1,3), (1,4), (1,5), (2,3), (2,4), (2,5) };
  2. B×A={(3,1), (3,2), (4,1), (4,2), (5,1), (5,2) };
复制代码
创建一个名为tb_course的表
  1. SELECT <字段名> FROM <表1> CROSS JOIN <表2> [WHERE子句];
复制代码
使用cross join查询出两张表中的笛卡尔积
  1. SELECT <字段名> FROM <表1>, <表2> [WHERE子句];
复制代码
使用,来查询两张表
  1. mysql> create database zxr;
  2. Query OK, 1 row affected (0.00 sec)
  3. mysql> use zxr;
  4. Database changed
  5. mysql> create table tb_students_info(id int not null primary key auto_increment,name varchar(50),age tinyint,sex varchar(4),height float,course_id int);
  6. Query OK, 0 rows affected (0.01 sec)
  7. mysql> desc tb_students_info;
  8. +-----------+-------------+------+-----+---------+----------------+
  9. | Field     | Type        | Null | Key | Default | Extra          |
  10. +-----------+-------------+------+-----+---------+----------------+
  11. | id        | int(11)     | NO   | PRI | NULL    | auto_increment |
  12. | name      | varchar(50) | YES  |     | NULL    |                |
  13. | age       | tinyint(4)  | YES  |     | NULL    |                |
  14. | sex       | varchar(4)  | YES  |     | NULL    |                |
  15. | height    | float       | YES  |     | NULL    |                |
  16. | course_id | int(11)     | YES  |     | NULL    |                |
  17. +-----------+-------------+------+-----+---------+----------------+
  18. 6 rows in set (0.00 sec)
  19. mysql> insert tb_students_info(name,age,sex,height,course_id) values('Dany',25,'M',160,1),('Green',23,'M',158,2),('Henry',23,'W',185,1),('Jane',22,'M',162,3),('Jim',24,'W',175,2),('John',21,'W',172,4),('Lily',22,'M',165,4),('Susan',23,'M',170,5),('Thomas',22,'W',178,5),('Tom',23,'W',165,5)
  20.     -> ;
  21. Query OK, 10 rows affected (0.00 sec)
  22. Records: 10  Duplicates: 0  Warnings: 0
  23. mysql> select * from tb_students_info;
  24. +----+--------+------+------+--------+-----------+
  25. | id | name   | age  | sex  | height | course_id |
  26. +----+--------+------+------+--------+-----------+
  27. |  1 | Dany   |   25 | M    |    160 |         1 |
  28. |  2 | Green  |   23 | M    |    158 |         2 |
  29. |  3 | Henry  |   23 | W    |    185 |         1 |
  30. |  4 | Jane   |   22 | M    |    162 |         3 |
  31. |  5 | Jim    |   24 | W    |    175 |         2 |
  32. |  6 | John   |   21 | W    |    172 |         4 |
  33. |  7 | Lily   |   22 | M    |    165 |         4 |
  34. |  8 | Susan  |   23 | M    |    170 |         5 |
  35. |  9 | Thomas |   22 | W    |    178 |         5 |
  36. | 10 | Tom    |   23 | W    |    165 |         5 |
  37. +----+--------+------+------+--------+-----------+
  38. 10 rows in set (0.01 sec)
复制代码
查询表中学习mysql的人的名字
  1. mysql> create table tb_course(id int not null primary key auto_increment,course_name varchar(50));
  2. Query OK, 0 rows affected (0.02 sec)
  3. mysql> desc tb_course;
  4. +-------------+-------------+------+-----+---------+----------------+
  5. | Field       | Type        | Null | Key | Default | Extra          |
  6. +-------------+-------------+------+-----+---------+----------------+
  7. | id          | int(11)     | NO   | PRI | NULL    | auto_increment |
  8. | course_name | varchar(50) | YES  |     | NULL    |                |
  9. +-------------+-------------+------+-----+---------+----------------+
  10. 2 rows in set (0.01 sec)
  11. mysql> select * from tb_course;
  12. +----+-------------+
  13. | id | course_name |
  14. +----+-------------+
  15. |  1 | Java        |
  16. |  2 | MySQL       |
  17. |  3 | Python      |
  18. |  4 | Go          |
  19. |  5 | C++         |
  20. +----+-------------+
  21. 5 rows in set (0.00 sec)
复制代码
查询表中id和course_id相同学习mysql的有哪些人
  1. mysql> select * from tb_course cross join tb_students_info;
  2. +----+-------------+----+--------+------+------+--------+-----------+
  3. | id | course_name | id | name   | age  | sex  | height | course_id |
  4. +----+-------------+----+--------+------+------+--------+-----------+
  5. |  1 | Java        |  1 | Dany   |   25 | M    |    160 |         1 |
  6. |  2 | MySQL       |  1 | Dany   |   25 | M    |    160 |         1 |
  7. |  3 | Python      |  1 | Dany   |   25 | M    |    160 |         1 |
  8. |  4 | Go          |  1 | Dany   |   25 | M    |    160 |         1 |
  9. |  5 | C++         |  1 | Dany   |   25 | M    |    160 |         1 |
  10. |  1 | Java        |  2 | Green  |   23 | M    |    158 |         2 |
  11. |  2 | MySQL       |  2 | Green  |   23 | M    |    158 |         2 |
  12. |  3 | Python      |  2 | Green  |   23 | M    |    158 |         2 |
  13. |  4 | Go          |  2 | Green  |   23 | M    |    158 |         2 |
  14. |  5 | C++         |  2 | Green  |   23 | M    |    158 |         2 |
  15. |  1 | Java        |  3 | Henry  |   23 | W    |    185 |         1 |
  16. |  2 | MySQL       |  3 | Henry  |   23 | W    |    185 |         1 |
  17. |  3 | Python      |  3 | Henry  |   23 | W    |    185 |         1 |
  18. |  4 | Go          |  3 | Henry  |   23 | W    |    185 |         1 |
  19. |  5 | C++         |  3 | Henry  |   23 | W    |    185 |         1 |
  20. |  1 | Java        |  4 | Jane   |   22 | M    |    162 |         3 |
  21. |  2 | MySQL       |  4 | Jane   |   22 | M    |    162 |         3 |
  22. |  3 | Python      |  4 | Jane   |   22 | M    |    162 |         3 |
  23. |  4 | Go          |  4 | Jane   |   22 | M    |    162 |         3 |
  24. |  5 | C++         |  4 | Jane   |   22 | M    |    162 |         3 |
  25. |  1 | Java        |  5 | Jim    |   24 | W    |    175 |         2 |
  26. |  2 | MySQL       |  5 | Jim    |   24 | W    |    175 |         2 |
  27. |  3 | Python      |  5 | Jim    |   24 | W    |    175 |         2 |
  28. |  4 | Go          |  5 | Jim    |   24 | W    |    175 |         2 |
  29. |  5 | C++         |  5 | Jim    |   24 | W    |    175 |         2 |
  30. |  1 | Java        |  6 | John   |   21 | W    |    172 |         4 |
  31. |  2 | MySQL       |  6 | John   |   21 | W    |    172 |         4 |
  32. |  3 | Python      |  6 | John   |   21 | W    |    172 |         4 |
  33. |  4 | Go          |  6 | John   |   21 | W    |    172 |         4 |
  34. |  5 | C++         |  6 | John   |   21 | W    |    172 |         4 |
  35. |  1 | Java        |  7 | Lily   |   22 | M    |    165 |         4 |
  36. |  2 | MySQL       |  7 | Lily   |   22 | M    |    165 |         4 |
  37. |  3 | Python      |  7 | Lily   |   22 | M    |    165 |         4 |
  38. |  4 | Go          |  7 | Lily   |   22 | M    |    165 |         4 |
  39. |  5 | C++         |  7 | Lily   |   22 | M    |    165 |         4 |
  40. |  1 | Java        |  8 | Susan  |   23 | M    |    170 |         5 |
  41. |  2 | MySQL       |  8 | Susan  |   23 | M    |    170 |         5 |
  42. |  3 | Python      |  8 | Susan  |   23 | M    |    170 |         5 |
  43. |  4 | Go          |  8 | Susan  |   23 | M    |    170 |         5 |
  44. |  5 | C++         |  8 | Susan  |   23 | M    |    170 |         5 |
  45. |  1 | Java        |  9 | Thomas |   22 | W    |    178 |         5 |
  46. |  2 | MySQL       |  9 | Thomas |   22 | W    |    178 |         5 |
  47. |  3 | Python      |  9 | Thomas |   22 | W    |    178 |         5 |
  48. |  4 | Go          |  9 | Thomas |   22 | W    |    178 |         5 |
  49. |  5 | C++         |  9 | Thomas |   22 | W    |    178 |         5 |
  50. |  1 | Java        | 10 | Tom    |   23 | W    |    165 |         5 |
  51. |  2 | MySQL       | 10 | Tom    |   23 | W    |    165 |         5 |
  52. |  3 | Python      | 10 | Tom    |   23 | W    |    165 |         5 |
  53. |  4 | Go          | 10 | Tom    |   23 | W    |    165 |         5 |
  54. |  5 | C++         | 10 | Tom    |   23 | W    |    165 |         5 |
  55. +----+-------------+----+--------+------+------+--------+-----------+
  56. 50 rows in set (0.00 sec)
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

鼠扑

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表