云计算学习100天-第47天-MySQL数据库学习3
命令语句扩展
比较符号:
= != > >= < <=
范围匹配:
in (值列表) //在…里
not in (值列表) //不在…里
between 数字1 and 数字2 //在…之间
示例:
//uid号表头的值 是 (1 , 3 , 5 , 7) 中的任意一个即可
select name , uid from tarena.user where uid in (1 , 3 , 5 , 7);
//shell 表头的的值 不是 "/bin/bash"或"/sbin/nologin" 即可
select name , shell from tarena.user where shell not in ("/bin/bash","/sbin/nologin");
//id表头的值 在 10 到 20 之间即可 包括 10 和 20 本身
select id , name , uid from tarena.user where id between 10 and 20 ;
where 字段名 like "表达式"; //模糊匹配
通配符
_ 表示 1个字符
% 表示零个或多个字符
示例:
//找名字必须是3个字符的 (没有空格挨着敲)
select name from tarena.user where name like "___";
//找名字以字母a开头的(没有空格挨着敲)
select name from tarena.user where name like "a%";
正则匹配:
where字段名 regexp '正则表达式'
正则表达式:
^ 匹配行首
$ 匹配行尾
[] 匹配范围内任意一个
* 前边的表达式出现零次或多次
| 或者
. 任意一个字符
多个判断条件:
逻辑与 and (&&) 多个判断条件必须同时成立
逻辑或 or (||) 多个判断条件其中某个条件成立即可
逻辑非 not (!) 取反
() 提高优先级
空 is null 表头下没有数据
非空 is not null 表头下有数据
示例:
select (2 + 3 ) * 5 ; //先加法再乘法
//注意null的大小写区分
insert into tarena.user(id,name) values(71,""); //零个字符
insert into tarena.user(id,name) values(72,"null"); //普通字母
insert into tarena.user(id,name) values(73,NULL); //表示空
insert into tarena.user(id,name) values(74,null); //表示空
别名 as
拼接 concat()
去重 distinct 字段名列表
示例:
select name as 用户名 , homedir 家目录 from tarena.user;
select concat(name,"-",uid) as 用户信息 from tarena.user where uid <= 5;
select distinct shell from tarena.user where shell in ("/bin/bash","/sbin/nologin") ;
分组 | 排序 | 过滤——
SELECT 表头名 FROM 库名.表名 [WHERE条件] group by | order by [DESC] | having;
DESC:降序排列
ASC: 升序排列 默认
Having一般用于group by 分组后的过滤
示例:
select shell as 解释器 , count(name) as 总人数 from tarena.user where shell in ("/bin/bash","/sbin/nologin") group by shell;
select name , uid from tarena.user where uid is not null and uid between 100 and 1000 order by uid asc;
select dept_id as 部门编号, count(name) as 总人数 from tarena.employees group by dept_id having 总人数 < 10 ;
分页——
SELECT语句 LIMIT 数字; //显示查询结果前多少条记录
SELECT语句 LIMIT 数字1,数字2; //显示指定范围内的查询记录
示例:
limit 1 ; 显示查询结果的第1行
limit 3 ; 显示查询结果的前3行
limit 10 ; 显示查询结果的前10行
limit 0,1 ; 从查询结果的第1行开始显示,共显示1行
limit 3,5 ; 从查询结果的第4行开始显示,共显示5行
limit 10,10; 从查询结果的第11行开始显示,共显示10行
更多推荐



所有评论(0)