select查询使用解析:
select 列名 from table_name;查询是列的情况,
若select 查询某列的指定格式数据,可以用表达式来表示
select avg(price> as '最大值' from table_name; as 可以命名这个查询结果显示列的名称
不等于<>
在两者之间between 350 and 450;
in()在什么之内
select * from readerinfo where name in('张飞','李月');
like查找姓张的两个字的人
select * from readerinfo where name like '张_';
查看135开头的
select * from readerinfo where tel like '135%';
逻辑运算符
and 与 select * from readerinfo where price>50 and price<70;
or 或
not 非
获取整数的函数
ceil(x):返回大于x的最小整数值
select ceil(28.55);结果29
floor(x):返回小于x的最大整数值。
select floor(28.55); 结果28
round(x):返回接近于参数x的整数,对参数进行四舍五入。
round(x,y):返回最接近于参数x的数,其值保留到小数点后面的y位,若y为负值,则将保留x值到小数点左边y位
select round(28.55,1)四舍五入1位,结果28.6
round(28.55,-1)对小数点前一位进行四舍五入,结果30
截断函数
truncate(x,y),舍去至小数点后y位的数字x.若y的值为0,则结果为整数。
select truncate(28.55,1) 结果28.5
select truncate(28.55,0) 结果28
select truncate(28.55,-1)结果20
select mod(x,y):x除以y得到的余数。mod(11,2) 结果:1
字符串连接函数
concat(s1,s2,...)返回结果为连接参数产生的字符串,如果任何一个参数为nuLl,则返回值为null.
select concat('hello','world');结果:helloworld;
concat(x,s1,s2,...)第一个参数x是其他参数的分隔符,分隔符的位置放在要连接的两个字符串之间,
lower(str)将字符串转为小写字母
upper(str)将字符串转为大写字母
ltrim(s)返回字符串s,字符串左侧空格被删除
rtrim(s)返回字符串s,字符串右侧空格被删除
trim(s)删除字符串两侧的空格
求字符串长度
length(str)
截取字符串
select substring(s,n,len)s字符串,n从n位置开始截取,len截取的长度
当n是负值时,是从字符串后面第n个字符开始截取
select substring('helloworld',-3,2);结果rl
获取指定长度的字符串函数
left(s,n)返回字符串s开始的最左边n个字符。
select left('hello world',5);结果:hello
right(s,n)返回字符串中最右边n个字符串
select right('hello world',5)结果:world
替换函数
replace(str,from_str,to_str);str为源字符串,from_str是str中所要被替换的字符串,to_str替换字符串。select replace('hello','h','w'); 结果:wello
格式化函数
format(x,n)将数字x格式化,并以四舍五入的方式保留小数点后n位,结果以字符串的形式返回。若n为0,则返回结果不含小数部分。
日期时间函数的使用
获取当前日期的函数
curdate()和current_date();以-07-01形式体现;curdate()+0;的形式0701形式
获取当前时间的函数
curtime()和current_time();11:18:48
获取当前日期和时间
now()和sysdate()
执行日期的加运算
date_add(date,interval expr type)date 是一个datetime或date值,用来指定起始时间,expr是一个表达式,用来指定从起始日期添加或减去的时间间隔值。type为关键字词,它指示了表达式被解释的方式:year,month,day,week,hour等
select date_add('-01-01',interval 5 month);
结果:-06-01
计算两个日期之间的间隔天数
datediff(date1,date2);
select datediff('-01-01','-02-01');
日期格式化
date_format(date,format)根据format指定的格式化显示date值。
%b:月份的缩写名称(jan...dec)
%c:月份的数字形式(0..12);
%m,月份,数字形式(00,...,12);
%M:月份名称(January...December)
%d:该月日期,数字形式(00,...31)
%e:该月日期,数字形式(0....31)
%Y:4位数形式表示年份
%y:2位数形式表示年份
select date_format('-04-01','%Y%c');
聚合函数
有时候并不需要返回实际表中的数据,而只是对获取的数据进行分析和总结。这时候需要使用聚合函数;
avg()返回某列的平均值
select avg(price) from book_info;
count()返回某列的行数
max()返回某列的最大值
min()返回某列的最小值
sum()返回某列值的和
系统信息函数
version():返回当前mysql服务器版本的版本号;
connection_id();返回mysql服务器当前连接的次数,每个连接都有各自唯一的id.
datebase()和schema():返回当前的数据库名
user():当前用户名的函数,返回当前登录的用户名称。
加密函数
md5();信息摘要算法
insert into myuser values('user1',md5('pwd1'));
md5(str)函数可以加密字符串,加密后的值以32位十六进制数字的二进制字符串形式返回,若参数为null,则返回null.
password():密码算法
password(str)从原明文密码str计算并返回加密后的密码字符串,当参数为null时,返回null
set password = password('rootpwd')修改密码
authentication_string 存储密码的列
select user,authentication_string from mysql.user;查看用户和密码
-------------------------------------------------
子查询
指嵌套在其他SQL语句内的查询语句,且必须始终出现圆括号内。
子查询的结果作为外层另一个查询的过滤条件
子查询可以嵌套update select delete insert等语句中
select * from book_info where book_category_id<>(select category_id from book_category where category='数据库');
any或some关键字
查看书价大于图书类别编号为4的任一图书价格的所有图书信息
select * from book_info where price>any(select price from book_info where book_category_id =4);any换成some也可以、、大于子查询的最小值
查看书价大于图书类别编号为4的全部图书价格的所有图书信息
select * from book_info where price>all(select price from book_info where book_category_id =4);//大于子查询的
in在什么范围内
select * from talbe_name where 列名 in 范围或(子查询);
exists 是否存在的意思
select * from book_info where exists (select * from book_info where boo_name='中医儿科学');
\c退出错误命令行