1.5.查询 2019/2/9
语法:
select 列,… from tb_name where con group by 列,… having con limit count;
1.单多表查询
单表查询:select * from tb_name; #查询所有字段
多表查询:select tb_1.id,tb_1.user,tb_2.id,tb_2.name from tb_1,tb_2;
视图查询:select * from (select no,name,open from m111) t where t.no>1;
指定别名:select id,name as n,sex as s from tb_name where id>1; #name,sex字段别名n,s
select * from tb_name p where p.talk=’php’; #p表的别名
去除重复:select distinct user,password from tb_name; #查询指定字段(去除重复)
结果排序:select * from tb_name order by asc[|desc];
限制查询:select *from tb_name limit 5; #前5行
select * from 表 limit 2,5; #从查询结果的第3行开始,显示5行
select * from 表 limit 5 offset 2; #从查询结果的第3行开始,显示5行
2.条件查询
select * from tb_name where id>1; #查询所有字段指定数据
select * from tb_name where user not in(‘Tom’,’Bob’); #集合查询;符型加单引号
select * from tb_name where id in (2,3,4) and date<'1988-12-1'; #集合查询
select * from tb_name where id not between 5 and 7; #范围查询
select * from tb_name where user not like ‘%Tom%’ having user like ‘T_m’;# 模糊查询_,%匹配单,多字符
select * from tb_name where name is not null;#非空查询
select * from tb_name where user=’Tom’and (name is null or id=<5);
3.分组查询:
用途:汇总统计多条结果(简单查询无法做到),常和聚合函数一起使用
格式:slect 要显示的结果字段 from tb_name [where 条件] group by 分组字段 [ordey by 字段 asc[|desc]];
参数:
要显示的结果字段:
id,user,count(id),sum(id),avg(id),min(id),max(id),group_concat(分组字段),…
说明:
group_concat(分组字段):将分组字段中的每个值都显示出来
显示的结果类型类似'm1901,m1901,m1901,m1901'
分组字段:字段1,字段2,…;先按字段1分组,当字段1的值相等时在按字段2分组
实例:
select 行业, avg(open) from tb_name group by 行业;#分组列一般按大类类别进行
select 行业,分类,name,count(open),sum(open),avg(open),min(open),max(open) from tb_name group by 分类;
select 行业,分类,name,count(open),avg(open) from tb_name where open is not null
group by name order by avg(open) desc;
select 行业,分类,name,vol from tb_name group by name,vol,行业; #多个字段分组
select 行业,分类,name,vol,group_concat(name) from tb_name group by name;#name必为分组字段
4.聚合函数 count,sum,min,max,avg,median,std:
说明:min,max参数可为str,date;median可为date;
select count(*) from tb_name; #统计表中总数不用where更快
select count(name) from tb_name; #统计表中name总数=6
select sum(id) as sumvalue from tb_name;#别名
select@min_price:=MIN(price),@max_price:=MAX(price) tb_name;
select * from new_futures.m111 order by rand() limit 5; #随机取出5条数据
select * from tb_namewhere datediff('minute',开始时间,getdate())>5;#延时查询
5.子查询(子查询返回结果是1个值时可以比较运算符;当返回列表时用in代替):
select * from tb_login where user in ( select user from tb_book);#user存在2个表中
select id,books,row from tb_book where row>=(select row from tb_row where id=1);# 比较查询
select * from tb_row where exists ( select * from tb_book where id=27) and row>90;# 内查询返回True值外查询才进行
select books,row from tb_book where id< any( select row from tb_row)
select books,row from tb_book where id>=all( select row from tb_row);
6.连接查询:
6.1内连接查询
select name,books from tb_login,tb_book where tb_login.user=tb_book.user and row>5;
6.2外连接查询left join;right join;
select name,books from tb_login left join tb_book on tb_login.user=tb.book.user
#name在表1中,books在表2中,user在两个表中;结果包含内连接数据+左表所有数据;并在右表相应列添加null值;
6.3全外连接:full/cross join不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
7.合并查询结果
union [all] 将所有查询结果合并去除相同记录;
except [all] 查询结果包括所有在table1中但不在table2 中的行并消除所有重复行
intersect [all] 查询结果包括所有在table1及table2 中的行并消除所有重复行
注:all 简单将结果合并不消除重复行;使用运算词的几个查询结果行必须是一致的。
select user from tb_1 union all select user from tb_2; #速度快于union
(select a from tableA) except (select a from tableB) except (select a from tableC)
#所有在TableA 中但不在 TableB和TableC 中的行并消除所有重复行
8.正则表达式查询:
select books from tb_book where books
regexp ‘^php’; #字符串开头
regexp ‘php$’; #字符串结尾
regexp ‘p.’; #匹配任意一个字符
regexp ‘[abc]’; #匹配abc中任意一个字符
regexp ‘[^a-z]’; #匹配除字符集外任意一个字符
regexp ‘s1|s2|s3’;#匹配任意一个字符串
regexp ‘j*a’; #匹配多个该字符a之前的字符j;j+a至少一个j
regexp ‘a{3}’; #匹配字符串连续出现3次
regexp ‘a{2,4}’; #匹配字符串最少2次,最多4次
9.判断函数 2019/2/10
if语句用法
if(expr,v1,v2) #如expr是true返回v1否则v2(数字或字符串,或字段)
select *,if(sex='1','男','女') as sex_cn from tb_user#查询结果添加一列sex_cn内容为男或女
select if(0.1,1,0); #0 #0.1被转化为整数值;v1,v2有null的返回非null
select if(0.1<>0,1,0); #1#比较检验了原始浮点值
ifnull语句用法
ifnull(v1,v2) #如v1不为空显示v1,否则v2(数字或字符串,或字段);
#默认结果值为两表达式中更加通用一个,顺序为str、real或integer
select ifnull(null,10); select ifnull(1/0,10); select ifnull(10,0); #10
select *,ifnull(user,'Tom') from tb_name #字段user为空就返回'Tom'
select *,ifnull(user,name) from tb_name where id in (5,6,12) #字段user为空就返回字段name
nullif语句用法
nullif(expr1,expr2)#如expr1 = expr2 成立返回null否则返回expr1
#等价case when expr1 = expr2 then null else expr1 end
select *,nullif(name,user) from tb_name#字段name与字段user相等返回空否则返回name
select nullif(1,1); #null
select nullif(1,2); #1
case语句用法
case when expr then v1[ when v1 then v2…] else vn end
#如expr=true返回v1否则v2(sql语句或者返回值); 用于sql语句和过程、触发器
select *,(case sex when '1' then '男' when '0' then '女' else '保密' end) as sex_cn from tb_name;
#仅能用相等判断 查询结果添加字段sex_cn显示为男或女或保密
select *,(case when sex='1' then '男' when sex='0' then '女' else '保密' end) as sex_cn
from tb_name order by sex_cn desc #比较判断 推荐用法
select *,(case when sex='1' then '男' when sex='0' then '女' else '保密' end) as sex_cn,
(case when age>=60 then '老年' when age<60 and age>=30 then '中年'
when age<30 and age>=18 then '青年' else '幼年' end) as age_cn from tb_name;
select count(*),(case 省份 when '北京' then '华北' when '广东' then '华南' when '湖南' then '华南' end) as area_cn
from tb_name group by area_cn; # 将区域分组统计华北华南注册人数
select count(case a.no when 7 then a.vol end) + count(case a.no when 11 then a.vol end) as rst_cn
from tb_name a where a.open = 3000# 查询tb_name表no为7或11,open = 3000 的所有记录数
select count(case a.no when 7 then a.vol end) as n1,
count(case a.no when 11 then a.vol end) as n2
from tb_name a where a.open = 3000;# 别查询tb_name表状态为7和11且open = 3000的所有记录数
10.实例:
#查看MYSQL数据库中所有用户
SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
#查看数据库中具体某个用户的权限
show grants for 'cactiuser'@'%';
select * from mysql.user where user='cactiuser' \G;
#查看数据库中指定表
SELECT TABLE_NAME FROM information_schema.tables where table_name = 'm1801' and table_schema = 'futures_test';
#查看指定数据库.数据表.列
select COLUMN_NAME from information_schema.COLUMNS where table_name = 'm1801'
and table_schema = 'futures_test';
1.查询数据表是否存在
set @str_tb_name= (SELECT TABLE_NAME FROM information_schema.tables
where table_name = 'm111' and table_schema = 'new_futures');#1条结果
set @a= (select if(@str_tb_name='m111','ok1','ng1') result);
2.查询数据列是否存在
#select (select COLUMN_NAME from information_schema.COLUMNS
where table_name = 'm111' and table_schema = 'new_futures' and column_name='No') into @b;#多条结果
set @str_col_name= (select COLUMN_NAME from information_schema.COLUMNS
where table_name = 'm111' and table_schema = 'new_futures' and column_name='No') ;#1条结果
set @b= (select if(@str_col_name='no','ok2','ng2') result) ;
3.显示结果:
select @a as a,@b as b,@str_tb_name as tb,@str_col_name as col;
"
a b tb col
ok1 ok2 m111 No
"