MySQL数据库入门--读书笔记(四)

第四章

简单查询:

         select[distinct] *|{字段名1,字段名2,字段名3,……}

                  from 表名

                  [where条件表达式1]

                  [groupby 字段名 [having 条件表达式2]]

                  [orderby 字段名 [asc|desc]]

                  [limit[offset] 记录数] 

         distinct:剔除查询结果中重复的数据

         groupby:将查询结果按照指定字段进行分组【不懂】

having:对分组后的结果进行过滤【不懂】

order by:指定排序

limit:offset表示偏移量,表示从第几个开始,记录数表示返回查询记录的条数。

查询所有字段:selectid,name,grade from student;

查询所有字段:使用通配符(*)

查询指定字段:select namefrom student;

按条件查询:

         带关系运算符的查询:selectid,name from student where id>5;

         带in关键字的查询:selectname from student where id in(4,5,6);

         带between and 关键字的查询:selectid,name from student where id [not] between 2 and 5;

         空值查询:selectid,name from student where name is [not] null;

         带distinct关键字的查询:selectdistinct name from student;【select distinct id,name from student;】

         带like关键字的查询:

1、 百分号(%)通配符:selectid,name from student where name [not] like’t%’;【匹配t开头的字符串】

select  id,name from student where name [not] like’s%t’;【匹配s开头t结尾的字符串】

select id,name from student wherename [not] like’%t%’;【匹配有t的字符串】

                  2、下划线_通配符:一个_只能匹配一个字符

                          selectid,name from student where name like’d_’;【匹配以d开头的两个字符】

                          selectid,name from student where name like’_d’;【匹配以d结尾的两个字符】

                  加”\”转义%和_

         带and关键字的多条件查询:selectid ,name from student where id in(1,2,3,4) and name like’%ing’;

         带or关键字的多条件查询:selectid,name from student id in(1,2,3) or name like’%d’;

         and可以和or一块使用,但是and的优先级要高于or

高级查询:

         聚合函数:

count():返回某列的行数select count(*) from student;

sum():返回某列值的和 select sum(score) from student;

avg():饭返回某列的平均值 select avg(score) from student;

max():返回某列的最大值select max(score) from student;

min():返回某列的最小值 select min(score) from student;

         对查询结果进行排序:

                  select* from student order by id asc,score desc;

         分组查询:

                  select* from student group by sex;【显示每个分组的一条记录】

                  selectcount(*),sex from student group by sex;【显示每个分组中记录的行数】

                  selectsum(score),sex  from student group by sexhaving sum(score)<300;【按照sex分组,查询出分组score总和小于300的分组】

         使用limit限制查询结果的数量:

                  select* from student order by score asc limit 4,4;【查询出按照成绩升序排列的第5~8条数据】

         函数:

                  数学函数:

abs(x):返回x的绝对值

sqrt(x):返回x的非负2次方根

mod(x,y):返回x被y除后的余数

ceiling(x):返回不小于x的最小整数

floor(x):返回不大于x的最大整数

round(x,y):对x进行四舍五入操作,小数点后保留y位

truncate(x,y):舍去x中小数点y位后面的数

sign(x):返回x的符号,-1,、0或者1

字符串函数:

         length(str):返回字符串str的长度

         concat(s1,s2,…):返回一个或者多个字符串连接产生的新的字符串

         trim(str):删除字符串两侧的空格

         replace(str,s1,s2):使用字符串s2替代字符串str中所有的字符串s1

         substring(str,n,len):返回字符串s1的子串,起始位置为n,长度为len

         reverse(str):返回字符串反转后的结果

         locate(s1,str):返回子串s1在字符串str中的起始位置

日期和时间函数:

         curdate():获取系统当前日期

         curtime():获取系统当前时间

         sysdate():获取当前系统日期和时间

         time_to_sec():返回将时间转换成秒的结果

条件判断函数:

         if(expr,v1,v2):如果expr表达式为true返回v1,否则返回v2

         ifnull(v1,v2):如果v1不为null返回v1,否则返回v2

         case expr where v1then r1[where v2 then r2…] [else rn] end:如果expr值等于v1、v2等,则返回对应位置then后面的结果,否则返回else后的结果rn

加密函数:

         md5(str):对字符串str进行md5加密

         encode(str,pwd_str):使用pwd作为密码加密字符串str

         decode(str,pwd_str):使用pwd作为密码解密字符串str

为表和字段取别名:

         为表取别名:select* from student as s where s.sex=’man’;【查询出表中性别为女的数据,同时为表取别名为s】

         为字段去别名:selectname as n,score s from student;【为name取别名为n,为score取别名为s】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值