mysql语句进阶

一、数据类型
MySQL中定义数据字段的类型对数据库的优化是非常重要的。
MySQL支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)0类型。
1、数值类型
在这里插入图片描述

2、日期和时间类型

在这里插入图片描述

3、字符串类型

在这里插入图片描述
整型
tinyint,占1字节,有符号:-138137,无符号位:0255
smallint,占2字节,有符号:-3276832767,无符号位:065535
mediumint,占3字节,有符号:-83886088388607,无符号位:016777215
int,占4字节,有符号:-21474836482147483647,无符号位:04284967295
bigint,占8字节
bool 等价于tinyint(1) 布尔型

浮点型
float([m[,d]]) 占4字节,1.17E-38~3.4E+38
double([m[,d]]) 占8字节
decimal([m[,d]]) 以字符串形式表示的浮点数

字符型
char([m]):固定长度的字符,占用m字节
varchar[(m)]:可变长度的字符,占用m+1字节,大于255个字符:占用m+2
tinytext,255个字符(2的8次方)
text,65535个字符(2的16次方)
mediumtext,16777215字符(2的24次方)
longtext,(2的32次方)
enum(value,value,…)占1/2个字节 最多可以有65535个成员
set(value,value,…)占1/2/3/4/8个字节,最多可以有64个成员

http://www.runoob.com/mysql/mysql-select-query.html
二、常用select命令
1、使用select命令查看Mysql数据库系统信息
例1:打印当前日期和时间
mysql> select now();
±--------------------+
| now() |
±--------------------+
| 2019-06-07 22:22:49 |
±--------------------+
1 row in set (0.00 sec)

例2:打印当前日期
mysql> select curdate();
±-----------+
| curdate() |
±-----------+
| 2019-06-07 |
±-----------+
1 row in set (0.00 sec)

例3:打印当前时间
mysql> select curtime();
±----------+
| curtime() |
±----------+
| 22:24:48 |
±----------+
1 row in set (0.00 sec)

例4:打印当前所在的数据库
mysql> select database();
±-----------+
| database() |
±-----------+
| HA |
±-----------+
1 row in set (0.00 sec)

例5:打印mysql数据库的版本
mysql> select version();
±----------+
| version() |
±----------+
| 5.7.26 |
±----------+
1 row in set (0.00 sec)

例6:打印当前用户
mysql> select user();
±---------------+
| user() |
±---------------+
| root@localhost |
±---------------+
1 row in set (0.00 sec)

2、使用show命令查看系统信息
例1:显示系统变量
mysql> show variables;

例2:显示全局变量
mysql> show global variables;

例3:使用模糊查询查询系统变量包含version的变量
mysql> show variables like “%version%”;
±------------------------±-----------------------------+
| Variable_name | Value |
±------------------------±-----------------------------+
| innodb_version | 5.7.26 |
| protocol_version | 10 |
| slave_type_conversions | |
| tls_version | TLSv1,TLSv1.1 |
| version | 5.7.26 |
| version_comment | MySQL Community Server (GPL) |
| version_compile_machine | x86_64 |
| version_compile_os | Linux |
±------------------------±-----------------------------+
8 rows in set (0.00 sec)

例4:使用模糊查询,查询系统变量默认存储引擎
mysql> show variables like “%storage_engine%”;
±---------------------------------±-------+
| Variable_name | Value |
±---------------------------------±-------+
| default_storage_engine | InnoDB |
| default_tmp_storage_engine | InnoDB |
| disabled_storage_engines | |
| internal_tmp_disk_storage_engine | InnoDB |
±---------------------------------±-------+
4 rows in set (0.00 sec)

注意:like和“_”模糊查询还可以用于where语句;
MySQL提供两个通配符,用于与LIKE运算符一起使用,它们分别是:百分比符号 %和下划线
百分比(%)表示通配符允许匹配任何字符串的零个或多个字符。
下划线(
)表示通配符允许匹配任何单个字符。

例5:查询stname包含zhang的列
mysql> select * from students;
±-----±---------±-----+
| id | stname | age |
±-----±---------±-----+
| 1 | zhangsan | 18 |
| 2 | wangwu | 25 |
| 3 | lisi | 30 |
| 4 | zhaoliu | 40 |
±-----±---------±-----+
4 rows in set (0.00 sec)

mysql> select stname from students where stname like “%zhang%”;
±---------+
| stname |
±---------+
| zhangsan |
±---------+
1 row in set (0.00 sec)

mysql> select age from students where age like “_8”; #单个字符匹配
±-----+
| age |
±-----+
| 18 |
±-----+
1 row in set (0.00 sec)

例6:查看支持那些存储引擎
mysql> show engines\G

例7:查询系统运行状态
mysql> show status;

例8:显示全局运行状态
mysql> show global status like “%Thread%”;
±-----------------------------------------±------+
| Variable_name | Value |
±-----------------------------------------±------+
| Delayed_insert_threads | 0 |
| Performance_schema_thread_classes_lost | 0 |
| Performance_schema_thread_instances_lost | 0 |
| Slow_launch_threads | 0 |
| Threads_cached | 0 |
| Threads_connected | 1 | #系统链接数
| Threads_created | 1 |
| Threads_running | 1 |
±-----------------------------------------±------+
8 rows in set (0.00 sec)

三、数据库的导出和导入
1、数据库的导出
语法:mysqldump -u 用户名 -p密码 数据库名 > 导出的文件名
注:导出的位置mysql用户需要有写入权限
[root@xuegod ~]# mysqldump -uroot -p123456 -B HA > /tmp/HA.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@xuegod ~]# ls /tmp/HA.sql -l
-rw-r–r--. 1 root root 2059 Jun 7 22:40 /tmp/HA.sql

扩展知识
mysqldump - u root -p123456 -B 库名 >文件.sql
-B : 导出整个库包含建库语句
mysqldump - u root -p123456 -A >文件.sql
-A:导出全部数据库

2、将select查询内容导出到外部文件
把一个select的结果导出到文本,其实就是备份数据库
mysql> use book;
mysql> select * into outfile ‘/tmp/133.txt’ from books; 此处有个文件访问权限问题,mysql用户是可以访问/tmp路径的,所以这里放到tmp下
或:
mysql> select * from books into outfile ‘/tmp/456.txt’;

注意:mysql 默认对导出的目录有权限限制,也就是说使用命令行进行导出的时候,需要指定目录进行操作,否则会出现报错如下。
mysql> select * into outfile ‘/tmp/133.txt’ from books;
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

扩展:
解决 5.7版本导出报错,可以设置my.cnf 加上secure-file-priv="/ "
[root@xuegod63 ~]# vim /etc/my.cnf #修改MySQL配置文件,在未行添加如下。
32 secure-file-priv="/tmp/" #追加这一行内容
保存后,重启mysql服务
[root@xuegod ~]# systemctl restart mysqld

例:将查询的内容导出到外部文件中
mysql> select * from students into outfile ‘/tmp/students.txt’;
Query OK, 4 rows affected (0.00 sec)
[root@xuegod ~]# cat /tmp/students.txt
1 zhangsan 18
2 wangwu 25
3 lisi 30
4 zhaoliu 40

3、数据库的导入
方法一:使用mysqldump命令导入
[root@xuegod ~]# ls
anaconda-ks.cfg book.sql
[root@xuegod ~]# mysql -e ‘create database book’ -uroot -p123456 #先创建book数据库
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@xuegod ~]# mysql -uroot -p123456 -B book < /root/book.sql #导入数据库
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@xuegod ~]# mysql -uroot -p123456
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 8
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright © 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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 |
| HA |
| book |
| mysql |
| performance_schema |
| sys |
±-------------------+
6 rows in set (0.00 sec)

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> show tables;
±---------------+
| Tables_in_book |
±---------------+
| books |
| category |
±---------------+
2 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)

方法二:使用source 数据库文件,导入数据库
mysql> create database book;
mysql> use book;
mysql> source /tmp/book.sql #sql脚本的路径
mysql> show tables;
±------------------+
| Tables_in_book |
±------------------+
| books |
| category |
±------------------+

四、SQL查询语句进阶
1、查看表的内容
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)

2、逻辑运算符
and or not 且 或 非
例1:选择出书籍价格为(30.40.50.60)的记录,只显示书籍名称,出版社,价格
mysql> select bName,publishing,price from books where price=30 or price=40 or price=50 or price=60;
±-------------------------------------±-------------------------±------+
| bName | publishing | price |
±-------------------------------------±-------------------------±------+
| Illustrator 10完全手册 | 科学出版社 | 50 |
| FreeHand 10基础教程 | 北京希望电子出版 | 50 |
| 网站设计全程教程 | 科学出版社 | 50 |
| ASP数据库系统开发实例导航 | 人民邮电出版社 | 60 |
| Delphi 5程序设计与控件参考 | 电子工业出版社 | 60 |
| ASP数据库系统开发实例导航 | 人民邮电出版社 | 60 |
±-------------------------------------±-------------------------±------+
6 rows in set (0.00 sec)

mysql> select bName,publishing,price from books where price in (30,40,50,60);
±-------------------------------------±-------------------------±------+
| bName | publishing | price |
±-------------------------------------±-------------------------±------+
| Illustrator 10完全手册 | 科学出版社 | 50 |
| FreeHand 10基础教程 | 北京希望电子出版 | 50 |
| 网站设计全程教程 | 科学出版社 | 50 |
| ASP数据库系统开发实例导航 | 人民邮电出版社 | 60 |
| Delphi 5程序设计与控件参考 | 电子工业出版社 | 60 |
| ASP数据库系统开发实例导航 | 人民邮电出版社 | 60 |
±-------------------------------------±-------------------------±------+
6 rows in set (0.00 sec)

3、算数运算符
= 等于
<> 不等于 !=

大于
< 小于
= 大于等于
<= 小于等于

in 运算符
IN 运算符用于 WHERE 表达式中,以列表项的形式支持多个选择,语法如下:
WHERE column IN (value1,value2,…)
WHERE column NOT IN (value1,value2,…)

Not in 与in相反
当 IN 前面加上 NOT 运算符时,表示与 IN 相反的意思,即不在这些列表项内选择。

例1:找出价格大于60的记录
mysql> select * from books where price>60;
±----±--------------------------------------------------------±--------±-------------------------±------±-----------±----------±------------+
| bId | bName | bTypeId | publishing | price | pubDate | author | ISBN |
±----±--------------------------------------------------------±--------±-------------------------±------±-----------±----------±------------+
| 28 | 活学活用Delphi5 | 5 | 人民邮电出版社 | 62 | 2003-05-01 | 付强 | 7505396293 |
| 104 | 2003-11-01 | 韩旭日 | 7505375458 |
| 44 | XML 完全探索 | 2 | 中国青年出版社 | 104 | 2004-01-01 | 齐鹏 | 7505357778 |
±----±--------------------------------------------------------±--------±-------------------------±------±-----------±----------±------------+
16 rows in set (0.00 sec)

例2:找出价格等于60的记录
mysql> select * from books where price=60;
±----±-------------------------------------±--------±----------------------±------±-----------±----------±-----------+
| bId | bName | bTypeId | publishing | price | pubDate | author | ISBN |
±----±-------------------------------------±--------±----------------------±------±-----------±----------±-----------+
| 26 | ASP数据库系统开发实例导航 | 2 | 人民邮电出版社 | 60 | 2006-01-01 | 刘刚 | 7505374710 |
| 27 | Delphi 5程序设计与控件参考 | 5 | 电子工业出版社 | 60 | 2003-02-01 | 孟卫峰 | 7505377353 |
| 34 | ASP数据库系统开发实例导航 | 2 | 人民邮电出版社 | 60 | 2006-01-01 | 刘刚 | 7505374710 |
±----±-------------------------------------±--------±----------------------±------±-----------±----------±-----------+
3 rows in set (0.00 sec)

例3:找出价格不等于60的记录
mysql> select bName,price from books where price<>60;
±--------------------------------------------------------±------+
| bName | price |
±--------------------------------------------------------±------+
| 网页样式设计-CSS | 45 |
| Internet操作技术 | 45 |
| Dreamweaver 4网页制作 | 45 |
±--------------------------------------------------------±------+
41 rows in set (0.00 sec) #41个记录,中间省略没有复制

例4:找出价格不是50,60,70的记录
mysql> select bName,price from books where price not in (50,60,70);
±--------------------------------------------------------±------+
| bName | price |
±--------------------------------------------------------±------+
| 网站制作直通车 | 34 |
| 黑客与网络安全 | 41 |
±--------------------------------------------------------±------+
38 rows in set (0.00 sec) #38个记录,省略没有复制

4、排序
升序:order by “排序字段” asc 默认不加asc也行
降序:order by “排序字段” desc
例1:找出价格是50,60,70的记录,升序排序
mysql> select bName,price from books where price in (50,60,70) order by price; #不加asc
±-------------------------------------±------+
| bName | price |
±-------------------------------------±------+
| Illustrator 10完全手册 | 50 |
| FreeHand 10基础教程 | 50 |
| 网站设计全程教程 | 50 |
| ASP数据库系统开发实例导航 | 60 |
| Delphi 5程序设计与控件参考 | 60 |
| ASP数据库系统开发实例导航 | 60 |
±-------------------------------------±------+
6 rows in set (0.00 sec)

例2:找出价格是50,60,70的记录,降序排序
mysql> select bName,price from books where price in (50,60,70) order by price desc;
±-------------------------------------±------+
| bName | price |
±-------------------------------------±------+
| ASP数据库系统开发实例导航 | 60 |
| Delphi 5程序设计与控件参考 | 60 |
| ASP数据库系统开发实例导航 | 60 |
| Illustrator 10完全手册 | 50 |
| FreeHand 10基础教程 | 50 |
| 网站设计全程教程 | 50 |
±-------------------------------------±------+
6 rows in set (0.01 sec)

例3:多次段排序
mysql> select bName,price from books where price in (50,60,70) order by price desc,bName desc; #对多个字段排序,中间用逗号分开
±-------------------------------------±------+
| bName | price |
±-------------------------------------±------+
| Delphi 5程序设计与控件参考 | 60 |
| ASP数据库系统开发实例导航 | 60 |
| ASP数据库系统开发实例导航 | 60 |
| 网站设计全程教程 | 50 |
| Illustrator 10完全手册 | 50 |
| FreeHand 10基础教程 | 50 |
±-------------------------------------±------+
6 rows in set (0.00 sec)

5、范围运算
[not]between …and…
Between and 可以使用大于小于的方式来代替,并且使用大于小于意义表述更明确

例1:查找价格不在30到60之间的书名和价格,并降序排序
mysql> select bName,price from books where price not between 30 and 60 order by price desc;
±--------------------------------------------------------±------+
| bName | price |
±--------------------------------------------------------±------+
| Javascript与Jscript从入门到精通 | 7500 |
| ASP 3初级教程 | 104 |
| XML 完全探索 | 104 |
| SQL Server 7.0数据库系统管理与应用开发 | 95 |
| SQL Server 2000 从入门到精通 | 93 |
| 3D Studio Max 3综合使用 | 91 |
| lllustrator 9宝典 | 83 |
| 3D MAX R3动画制作与培训教程 | 73 |
| HTML设计实务 | 72 |
| Frontpage 2000& ASP 网页设计技巧与网站维护 | 71 |
| 深入Flash 5教程 | 64 |
| Auto CAD R14 中文版实用教程 | 64 |
| Auto CAD 2002 中文版实用教程 | 63 |
| 3DS MAX 4横空出世 | 63 |
| 精通Javascript | 63 |
| 活学活用Delphi5 | 62 |
±--------------------------------------------------------±------+
16 rows in set (0.00 sec)

mysql> select bName,price from books where price<30 or price>60 order by price desc;
±--------------------------------------------------------±------+
| bName | price |
±--------------------------------------------------------±------+
| Javascript与Jscript从入门到精通 | 7500 |
| ASP 3初级教程 | 104 |
| XML 完全探索 | 104 |
| SQL Server 7.0数据库系统管理与应用开发 | 95 |
| SQL Server 2000 从入门到精通 | 93 |
| 3D Studio Max 3综合使用 | 91 |
| lllustrator 9宝典 | 83 |
| 3D MAX R3动画制作与培训教程 | 73 |
| HTML设计实务 | 72 |
| Frontpage 2000& ASP 网页设计技巧与网站维护 | 71 |
| 深入Flash 5教程 | 64 |
| Auto CAD R14 中文版实用教程 | 64 |
| Auto CAD 2002 中文版实用教程 | 63 |
| 3DS MAX 4横空出世 | 63 |
| 精通Javascript | 63 |
| 活学活用Delphi5 | 62 |
±--------------------------------------------------------±------+
16 rows in set (0.00 sec)

6、模糊匹配查询
字段名 [not]like ‘通配符’ ----》% 表示任意多个字符
例1:查找书名中包括“程序”字样记录
mysql> select bName,price from books where bName like “%程序%”;
±------------------------------------±------+
| bName | price |
±------------------------------------±------+
| 网络程序与设计-asp | 43 |
| Delphi 5程序设计与控件参考 | 60 |
±------------------------------------±------+
2 rows in set (0.00 sec)

例2:查找书名中不包含程序的字样
mysql> select bName,price from books where bName not like “%程序%”;
±--------------------------------------------------------±------+
| bName | price |
±--------------------------------------------------------±------+
| 网站制作直通车 | 34 |
| 黑客与网络安全 | 41 |
| ASP 3初级教程 | 104 |
| XML 完全探索 | 104 |
±--------------------------------------------------------±------+
42 rows in set (0.00 sec) #中间省略

7、mysql子查询
概念:在select 的where条件中又出现了select,查询中嵌套着查询
例1:选择类型名为“网络技术”的图书
mysql> select bTypeId from category where bTypeName=‘网络技术’;
±--------+
| bTypeId |
±--------+
| 7 |
±--------+
1 row in set (0.00 sec)
嵌套查询:
mysql> select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeNamee=‘网络技术’);
±---------------------±--------+
| bName | bTypeId |
±---------------------±--------+
| Internet操作技术 | 7 |
±---------------------±--------+
1 row in set (0.00 sec)

例2:选择类型命令为黑客的图书
mysql> select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName=‘黑客’);
±-------------------------±--------+
| bName | bTypeId |
±-------------------------±--------+
| 黑客与网络安全 | 6 |
| 黑客攻击防范秘笈 | 6 |
±-------------------------±--------+
2 rows in set (0.00 sec)

五、limit限定显示条目
语法:SELECT * FROM table LIMIT [offset,] rows
偏移量 行数
  LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1)。

比如select * from table limit m,n语句
表示其中m是指记录开始的index,从0开始,表示第一条记录
n是指从第m+1条开始,取n条。

例1:查询category表中第2到第6条数据
2-6,有5行数据,从第2开始,所以是1,5
mysql> select * from category limit 1,5;
±--------±-------------+
| bTypeId | bTypeName |
±--------±-------------+
| 2 | 网站 |
| 3 | 3D动画 |
| 4 | linux学习 |
| 5 | Delphi学习 |
| 6 | 黑客 |
±--------±-------------+
5 rows in set (0.00 sec)

例2:查看所有书籍中价格最低的三条记录
mysql> select bName,price from books order by price asc limit 3;
±----------------------------±------+
| bName | price |
±----------------------------±------+
| 网站制作直通车 | 34 |
| 黑客与网络安全 | 41 |
| 网络程序与设计-asp | 43 |
±----------------------------±------+
3 rows in set (0.00 sec)

例3:显示字段bName,price;条件:找出价格比电子工业出版社出版的书中最便宜还要便宜的书
mysql> select price from books where publishing=‘电子工业出版社’ order by price limit 1;
±------+
| price |
±------+
| 60 |
±------+
1 row in set (0.00 sec)

mysql> select bName,price from books where price<(select price from books where publishing=‘电子工业出版社’ order by price limit 1);
±-------------------------------------------------------±------+
| bName | price |
±-------------------------------------------------------±------+
| 网站制作直通车 | 34 |
| Auto CAD 2000 应用及实例基集锦 | 58 |
| Access 2000应用及实例基集锦 | 59 |
±-------------------------------------------------------±------+
25 rows in set (0.00 sec)

多行子查询
all表示小于子查询中返回全部值中的最小值
mysql> select bName,price from books where price<all(select price from books where publishing=‘电子工业出版社’ order by price);
±-------------------------------------------------------±------+
| bName | price |
±-------------------------------------------------------±------+
| 网站制作直通车 | 34 |
| 黑客与网络安全 | 41 |
| Access 2000应用及实例基集锦 | 59 |
±-------------------------------------------------------±------+
25 rows in set (0.00 sec)

六、连接查询
以一个共同的字段,求两张表当中符合条件的并集。通过共同字段把这两张表连接起来。
常用的连接:
内连接:根据表中的共同字段进行匹配
外连接分两种:左外连接、右外链接。
1、内连接
语法:select 字段 from 表1 inner join 表2 on 表1.字段=表2.字段
内连接:根据表中的共同字段进行匹配
mysql> select a.bname,a.price,b.btypename from books a inner join category b on a.btypeid=b.btypeid;
±--------------------------------------------------------±------±--------------+
| bname | price | btypename |
±--------------------------------------------------------±------±--------------+
| 网站制作直通车 | 34 | 网站 |
| 黑客与网络安全 | 41 | 黑客 |
| 网络程序与设计-asp | 43 | 网站 |
| 3D MAX 3.0 创作效果百例 | 45 | 3D动画 |
| ASP 3初级教程 | 104 | 网站 |
| XML 完全探索 | 104 | 网站 |
±--------------------------------------------------------±------±--------------+
44 rows in set (0.00 sec)
mysql> select a.bname,a.price,b.btypename from books a, category b where a.btypeid=b.btypeid;

2、外连接
外连接 (分为左外连接;右外连接)
1)左连接:
语法:select 字段 from a表 left join b表 on 连接条件
解释:优先显示左表全部记录,此时左表主表,右表为从表
主表内容全都有,从表内没有的显示null
mysql> select a.bname,a.price,b.btypename from books a left join category b on a.btypeid=b.btypeid;
±--------------------------------------------------------±------±--------------+
| bname | price | btypename |
±--------------------------------------------------------±------±--------------+
| 网站制作直通车 | 34 | 网站 |
| HTML设计实务 | 72 | 网站 |
| XML 完全探索 | 104 | 网站 |
。。。
±--------------------------------------------------------±------±--------------+
44 rows in set (0.00 sec)

2)右连接
语法:select 字段 from a表 right join b表 on 条件
解释:优先显示右表全部记录,此时右表主表,左表为从表
主表内容全都有,从表内没有的显示null。
mysql> select a.bname,b.* from books a right join category b on a.btypeid=b.btypeid;
±--------------------------------------------------------±--------±--------------+
| bname | bTypeId | bTypeName |
±--------------------------------------------------------±--------±--------------+
| 网站制作直通车 | 2 | 网站 |
| ASP 3初级教程 | 2 | 网站 |
| XML 完全探索 | 2 | 网站 |
。。。
±--------------------------------------------------------±--------±--------------+
44 rows in set (0.00 sec)

七、聚合函数
1、算数运算函数
1)计算books表中记录的价格的和
mysql> select sum(price) from books;
±-----------+
| sum(price) |
±-----------+
| 10048 |
±-----------+
1 row in set (0.00 sec)

2)求id小于3的所有书籍价格的平均值
mysql> select avg(price) from books where bId<=3;
±-----------+
| avg(price) |
±-----------+
| 39.3333 |
±-----------+
1 row in set (0.00 sec)
3)求价格的最大值
mysql> select max(price) from books;
±-----------+
| max(price) |
±-----------+
| 7500 |
±-----------+
1 row in set (0.00 sec)

mysql> select bname,price from books order by price desc limit 1;
±---------------------------------------±------+
| bname | price |
±---------------------------------------±------+
| Javascript与Jscript从入门到精通 | 7500 |
±---------------------------------------±------+
1 row in set (0.00 sec)

4)求价格最小值
mysql> select min(price) from books;
±-----------+
| min(price) |
±-----------+
| 34 |
±-----------+
1 row in set (0.00 sec)

mysql> select bname,price from books order by price limit 1;
±----------------------±------+
| bname | price |
±----------------------±------+
| 网站制作直通车 | 34 |
±----------------------±------+
1 row in set (0.00 sec)

2、统计记录数
1)统计价格大于40的书籍数量
mysql> select count(price) from books where price>40;
±-------------+
| count(price) |
±-------------+
| 43 |
±-------------+
1 row in set (0.00 sec)

2)价格去重后,在计算个数
mysql> select count(distinct price) from books where price>40;
±----------------------+
| count(distinct price) |
±----------------------+
| 26 |
±----------------------+
1 row in set (0.00 sec)

3、算数运算
例1:给所有价格小于40的书籍,涨价5元
mysql> update books set price=price+5 where price<40;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

例2:给所有价格高于70元的书籍打8折
mysql> update books set price=price*0.8 where price>70;
Query OK, 10 rows affected (0.00 sec)
Rows matched: 10 Changed: 10 Warnings: 0

4、字符串函数
1)语法:substr(string ,start,len)
截取:从start开始,截取len长
比如:1,7 从1开始,截取7个字符
mysql> select substr(btypename,1,7) from category where btypeid=10;
±----------------------+
| substr(btypename,1,7) |
±----------------------+
| AutoCAD | #本来是AutoCAD技术
±----------------------+
1 row in set (0.00 sec)

mysql> select substr(btypename,8,2) from category where btypeid=10;
±----------------------+
| substr(btypename,8,2) |
±----------------------+
| 技术 |
±----------------------+
1 row in set (0.00 sec)

3)拼接concat
语法:concat(str1,str2,str3…) 拼接,把多个字段拼成一个字段输出
mysql> select concat(bname,publishing) from books limit 1;
±----------------------------------------------+
| concat(bname,publishing) |
±----------------------------------------------+
| 网站制作直通车电脑爱好者杂志社 |
±----------------------------------------------+
1 row in set (0.00 sec)

mysql> select concat(bname,’-------’,publishing) from books limit 1;
±-----------------------------------------------------+
| concat(bname,’-------’,publishing) |
±-----------------------------------------------------+
| 网站制作直通车-------电脑爱好者杂志社 |
±-----------------------------------------------------+
1 row in set (0.00 sec)

5、大小写转换
upper()大写 : 转为大写输出
mysql> select upper(bname) from books where bid=9;
±--------------------------+
| upper(bname) |
±--------------------------+
| DREAMWEAVER 4网页制作 |
±--------------------------+
1 row in set (0.00 sec)

lower()小写:转为小写输出
mysql> select lower(bname) from books where bid=9;
±--------------------------+
| lower(bname) |
±--------------------------+
| dreamweaver 4网页制作 |
±--------------------------+
1 row in set (0.00 sec)

MySQL语句进阶可以涉及以下几个方面: 1. 子查询:子查询是将一个查询嵌套在另一个查询中的查询。它可以用于获取更复杂的结果集或根据子查询的结果进行过滤。例如: ```sql SELECT column1 FROM table1 WHERE column2 IN (SELECT column3 FROM table2); ``` 2. 联结(JOIN):联结是将两个或多个表中的行组合在一起以获取更全面的结果集。常见的联结类型有内联结(INNER JOIN)、左联结(LEFT JOIN)、右联结(RIGHT JOIN)和全联结(FULL JOIN)。例如: ```sql SELECT column1, column2 FROM table1 INNER JOIN table2 ON table1.column = table2.column; ``` 3. 窗口函数:窗口函数是一种在查询结果上执行计算的函数,它可以对分组或排序后的结果集进行操作。常见的窗口函数有排名函数(RANK)、累计函数(SUM、AVG)和偏移函数(LEAD、LAG)。例如: ```sql SELECT column1, column2, RANK() OVER (PARTITION BY column3 ORDER BY column4 DESC) AS rank FROM table1; ``` 4. 存储过程:存储过程是一组预编译的SQL语句,它可以接收参数并在数据库中执行。存储过程可以用于实现复杂的业务逻辑和提高查询性能。例如: ```sql CREATE PROCEDURE procedure_name (IN param1 INT, OUT param2 VARCHAR(255)) BEGIN -- 执行SQL语句 END; ``` 5. 触发器(Trigger):触发器是与表相关联的一种特殊类型的存储过程,它在特定的数据库操作(如插入、更新、删除)发生时自动执行。触发器可以用于实现数据约束、审计跟踪和数据同步等功能。例如: ```sql CREATE TRIGGER trigger_name AFTER INSERT ON table1 FOR EACH ROW BEGIN -- 执行SQL语句 END; ``` 这些是MySQL语句的一些进阶技巧,希望能对你有所帮助。如果你还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值