Mysql数据库(5)

目录

        一、sql高级语言

        1.1 导入数据库

         1.2 select 查询

         1.3 distinct 查询

         1.4 where 查询

         1.5 and与or

         1.6 in 用法

         1.7 between用法

         1.8 like 通配符

        1.9 order by 用法

         二、 sql函数

        2.1 函数

        2.2 聚合函数

        2.3 字符串函数

        2.4 group by 用法

         2.5 having 用法


        一、sql高级语言

        1.1 导入数据库

    mysql> source /root/test.sql   ##登录数据库将文件放入系统中再导入数据库

         1.2 select 查询

显示表格中的一个或者多个字段中所有的信息
语法:
select 字段名  from 表名;

例如:

mysql> select * from students;  ##查询student这个表中的所有信息

mysql> select name from students; ##查询students这个表中的name

##如果数据量巨大慎用*所有

 

         1.3 distinct 查询

distinct 查询不重复记录
语法:
select distinct 字段 from 表名

例如:

mysql> select distinct age from students; ##查询students表中不重复age字段

         1.4 where 查询

where 有条件的查询
语法:select '字段' from 表名  where 条件

例如:

mysql> select name,age from students where age<20;#显示name和age 并且要找到age 小于20的

 

         1.5 and与or

and 且  、or  或
语法:
select 字段名  from 表名 where 条件1 (and|or) 条件2 (and|or)条件3;

例如:

mysql> select name,age from students where age>20 and age<30; ##查询这表中的20-30岁之间的并显示姓名和年龄;

mysql> select name,age from students where gender='f' or (age>30 and age<20);
 

         1.6 in 用法

in:
显示已知值的资料
语法:
select 字段名  from 表名 where 字段 in ('值1','值2'....);

例如:

mysql> select * from students where classid in ('1','2');##查询这个表中所有classid为1和2的数据

 

         1.7 between用法

between:
显示两个值范围内的资料
语法:
select 字段名  from 表名 where 字段 between  '值1' and '值2';

例如:

mysql> select * from students where age between '17' and '20'; ##查询这个表中的年龄为17到20岁之间

##适用于数值查询,不适用于字符查询

 

         1.8 like 通配符

通配符通常是和  like 一起使用
语法:
select 字段名  from 表名 where 字段 like 模式

例如;

mysql> select * from students where name like 'l%';

mysql> select * from students where name like 'l_n%';
mysql> select * from students where name like 'xu%';
mysql> select * from students where name like '%ing';
mysql> select * from students where name like '%ng%';

mysql> select * from students where name like '_hi%';

 

通配符含义
%表示零个,一个或者多个字符
_下划线表示单个字符
A_Z所有以A开头 Z 结尾的字符串 'ABZ' 'ACZ' 'ACCCCZ'不在范围内 下划线只表示一个字符 AZ 包含a空格z
ABC%所有以ABC开头的字符串 ABCD ABCABC
%CBA所有以CBA结尾的字符串 WCBA CBACBA
%AN%所有包含AN的字符串 los angeles
_AN%所有 第二个字母为 A 第三个字母 为N 的字符串

        1.9 order by 用法

order by 按关键字排序
语法:
select 字段名  from 表名 where 条件 order by 字段 [asc,desc]; ##加入desc时是反向

例如:

select name,age from students order by age;

mysql> select name,age from students order by age desc;

mysql> select * from students where classid=1;
 

 

         二、 sql函数

        2.1 函数

函数含义
abs(x)返回x 的 绝对值
rand()返回0到1的随机数
mod(x,y)返回x除以y以后的余数
power(x,y)返回x的y次方
round(x)返回离x最近的整数
round(x,y)保留x的y位小数四舍五入后的值
sqrt(x)返回x的平方根
truncate(x,y)返回数字 x 截断为 y 位小数的值
ceil(x)返回大于或等于 x 的最小整数
floor(x)返回小于或等于 x 的最大整数
greatest(x1,x2.....)返回返回集合中最大的值
least(x1,x2..........)返回返回集合中最小的值

       例如:

mysql> select abs(2); ##返回绝对值
+--------+
| abs(2) |
+--------+
|      2 |
+--------+
1 row in set (0.00 sec)

mysql> select rand(); ##返回随机数
+--------------------+
| rand()             |
+--------------------+
| 0.8113523766504157 |
+--------------------+
1 row in set (0.00 sec)

mysql> select mod(6,4);##返回相除的数
+----------+
| mod(6,4) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)

mysql> select power(2,2); ##返回2的2次方
+------------+
| power(2,2) |
+------------+
|          4 |
+------------+
1 row in set (0.00 sec)

mysql> select round(2.5); ##保留2.5四舍五入的值
+------------+
| round(2.5) |
+------------+
|          3 |
+------------+
1 row in set (0.00 sec)

mysql> select round(3.1415926345,3); ##保留3.1415926345四舍五入的值
+-----------------------+
| round(3.1415926345,3) |
+-----------------------+
|                 3.142 |
+-----------------------+
1 row in set (0.00 sec)

mysql> select truncate(3.1415926345,3); ##保留小数第三位
+--------------------------+
| truncate(3.1415926345,3) |
+--------------------------+
|                    3.141 |
+--------------------------+
1 row in set (0.00 sec)

mysql> select ceil(2.6); ##返回大于等于2.6的最小整数
+-----------+
| ceil(2.6) |
+-----------+
|         3 |
+-----------+
1 row in set (0.00 sec)

mysql> select floor(2.4); ##返回小于或等于2.4的最小整数
+------------+
| floor(2.4) |
+------------+
|          2 |
+------------+
1 row in set (0.00 sec)

mysql> select least(22,33,44);##返回返回集合中最小的值
+-----------------+
| least(22,33,44) |
+-----------------+
|              22 |
+-----------------+
1 row in set (0.00 sec)

mysql> select greatest(55,66,88); ##返回返回集合中最大的值
+--------------------+
| greatest(55,66,88) |
+--------------------+
|                 88 |
+--------------------+
1 row in set (0.00 sec)

mysql> 

        2.2 聚合函数

函数含义
avg()返回指定列的平均值
count()返回指定列中非 NULL 值的个数
min()返回指定列的最小值
max()返回指定列的最大值
sum()返回指定列的所有值之和
mysql> select avg(age) from students;  ##求出平均年龄
+----------+
| avg(age) |
+----------+
|  27.4000 |
+----------+
1 row in set (0.00 sec)

mysql> select sum(age) from students; ##求出年龄和
+----------+
| sum(age) |
+----------+
|      685 |
+----------+
1 row in set (0.00 sec)

mysql> select max(age) from students; ##求出最大值
+----------+
| max(age) |
+----------+
|      100 |
+----------+
1 row in set (0.00 sec)

mysql> select min(age) from students; ##求出最小值
+----------+
| min(age) |
+----------+
|       17 |
+----------+
1 row in set (0.00 sec)

mysql> select count(classid) from students; ##求出非空字段
+----------------+
| count(classid) |
+----------------+
|             23 |
+----------------+

mysql> select name,classid from students where classid is null; ##查看这个表中classid为空的数据并查看显示姓名
+-------------+---------+
| name        | classid |
+-------------+---------+
| Xu Xian     |    NULL |
| Sun Dasheng |    NULL |
+-------------+---------+
2 rows in set (0.00 sec)

mysql> 

        2.3 字符串函数

函数描述
trim()返回去除指定格式的值
concat(x,y)将提供的参数 x 和 y 拼接成一个字符串
substr(x,y)获取从字符串 x 中的第 y 个位置开始的字符串,跟substring()函数作用相同
substr(x,y,z)获取从字符串 x 中的第 y 个位置开始长度为z 的字符串
length(x)返回字符串 x 的长度
replace(x,y,z)将字符串 z 替代字符串 x 中的字符串 y
upper(x)将字符串 x 的所有字母变成大写字母
lower(x)将字符串 x 的所有字母变成小写字母
left(x,y)返回字符串 x 的前 y 个字符
right(x,y)返回字符串 x 的后 y 个字符
repeat(x,y)将字符串 x 重复 y 次
space(x)返回 x 个空格
strcmp(x,y)比较 x 和 y,返回的值可以为-1,0,1
reverse(x)将字符串 x 反转

        例如:

mysql> select trim(leading 'Sun ' from 'Sun Dasheng'); ##除去sun与空格记录,区分大小写
+-----------------------------------------+
| trim(leading 'Sun ' from 'Sun Dasheng') |
+-----------------------------------------+
| Dasheng                                 |
+-----------------------------------------+
1 row in set (0.00 sec)

mysql> 
mysql> select trim(both 'Sun ' from 'Sun Dasheng');  ##除去sun与空格记录,区分大小写
+--------------------------------------+
| trim(both 'Sun ' from 'Sun Dasheng') |
+--------------------------------------+
| Dasheng                              |
+--------------------------------------+
1 row in set (0.00 sec)



mysql> select name,length(name) from students; #计算出字段中记录的字符长度
+---------------+--------------+
| name          | length(name) |
+---------------+--------------+
| Shi Zhongyu   |           11 |
| Shi Potian    |           10 |
| Xie Yanke     |            9 |
| Ding Dian     |            9 |
| Yu Yutong     |            9 |
| Shi Qing      |            8 |
| Xi Ren        |            6 |
| Lin Daiyu     |            9 |
| Ren Yingying  |           12 |
| Yue Lingshan  |           12 |
| Yuan Chengzhi |           13 |
| Wen Qingqing  |           12 |


mysql> select replace(name,'Shi Zhongyu','Kong')from students;  ##将数据替换并展示
+------------------------------------+
| replace(name,'Shi Zhongyu','Kong') |
+------------------------------------+
| Kong                               |


mysql> select concat(age,name) from students;  ##查询并显示age,name数据
+------------------+
| concat(age,name) |
+------------------+
| 22Shi Zhongyu    |
| 22Shi Potian     |
| 53Xie Yanke      |



mysql> select substr(name,7) from students where name='Sun Dasheng'; ##查询name字段从第七个字母开始并显示记录,空行算一个字符
+----------------+
| substr(name,7) |
+----------------+
| sheng          |
+----------------+
1 row in set (0.00 sec)



        2.4 group by 用法

语法:
select 字段1,sum(字段2) from 表名 group by 字段1

mysql> select classid,sum(age) from students group by classid; ##查询classid分组并求出年龄的和
+---------+----------+
| classid | sum(age) |
+---------+----------+
|    NULL |      127 |
|       1 |       82 |
|       2 |      108 |
|       3 |       81 |
|       4 |       99 |
|       5 |       46 |
|       6 |       83 |
|       7 |       59 |

         2.5 having 用法

having:用来过滤由group by语句返回的记录集,通常与group by语句联合使用
having语句的存在弥补了where关键字不能与聚合函数联合使用的不足。如果被SELECT的只有函数栏,那就不需要GROUP BY子句。
语法:SELECT 字段1,SUM("字段")FROM 表格名 GROUP BY 字段1 having(函数条件);

mysql> select classid,max(age) from students group by classid having classid<3;
+---------+----------+
| classid | max(age) |
+---------+----------+
|       1 |       22 |
|       2 |       53 |
+---------+----------+
2 rows in set (0.00 sec)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值