表记录的基本操作
查看 selectselect 字段名列表 from 数据库名.表名;select 字段名列表 from 数据库名.表名 where 条件表达式;*字段名1,字段名2,字段名N条件的表示方式:使用where引导,适用于更新、删除、查询等场合,符合条件的记录才被操作1.数值比较等于: = 不等于: !=大于、大于且等于: > >= 小于、小于且等于 < <=在..与..之间:between .. and ..2. 逻辑比较 (查询条件是2个或2个以上时使用)and 并且 多个条件同时成立or 或 多个条件只要有一个条件成立就可以! 非 ( 取反) = !=select * from usertab where username="root" and uid=20;select * from usertab where username!="root" and uid!=20;3.范围内查找(1)in 在....里select username,uid,shell from usertab where uid in (500,501,10);select username,uid,shell from usertab where username in ("root","bin","sync");select username,uid,shell from usertab where uid=500 or uid=501 or uid=10;(2)not in 不在....里select username,uid,shell from usertab where uid not in (500,501,10);(3)between .... and .... 同时匹配两个条件select * from usertab where uid>=5 and uid<=10;select * from usertab where uid between 5 and 10;select username from usertab where username between "a" and "f";(4)is null 空 (5) is not null 非空 select * from usertab where shell is not null;4.排序order by 升序排序:asc ( 默认 )降序排序:descorder by 字段名 desc样例:把uid号最大的前5个用户信息输出select * from usertab order by uid desc limit 5;select * from usertab limit 1;5.设置显示记录的条目数limit limit N,Mlimit MN 从查询结果的第几条记录开始显示,不指定N的值,默认从查询结果的第一条 记录开始显示,第一条记录的编号是0M 共显示几条记录select * from usertab limit 2;select * from usertab limit 5,6;select * from usertab order by uid desc limit 5;6.分组group by group by 字段名select shell from usertab group by shell;select shell from usertab where uid>=500 group by shell;7.不显示字段的重复值 DISTINCT select distinct shell from usertab;8.模糊查询like % 匹配零个到多个字符_ 匹配任意一个字符select * from usertab where username like "r%";select * from usertab where username like "___";select * from usertab where username like "r___";select * from usertab where username like "_r_";select * from usertab where username like "r%r";select * from usertab where username like "_%_";9.正则表达式 regexpWHERE 字段名 REGEXP '正则表达式' ^ 匹配开头$ 匹配结尾. 匹配任意当个字符[0-9] 匹配所有数字[0123456789][a-z]select username from usertab where username regexp 'o';select username from usertab where username regexp '...';select username from usertab where username regexp '^....$';select username from usertab where username regexp '^a...$';select username from usertab where username regexp '^s';select username from usertab where username regexp '[0-9]';10.四则运算求年龄:alter table usertab add syear year default "1987" after username;select username,2014-syear as age from usertab;select username,uid+gid as zcj from usertab; select username,(uid+gid)/2 as pjcj from usertab; 11.算数运算函数对集合中的各参数求和:sum(字段名)集合的平均值:avg(字段名)集合中的最小值:min(字段名)集合中的最大值:max(字段名)记录的个数:count(字段名)样例select sum(字段名) from 表; --------------------------------------------------------------------