记录操作:
增
# 添加记录
insert into 表明 set 字段=值; #(值是字符时需要加"")
eg. insert into t1 set id=1,name='xiaoming';
insert into 表名 values(值1,值2,....); #顺序按照表头的顺序(字符需要加引号)
eg. insert into t2 values(2,'male','zhao',88);
insert into t2 (id,name,linux) values(3,'qian',90);
insert into t2 (id,name,linux) values(4,'sun',93),(5,'li',76);
删
# 删除记录,一定要有定位
delete from t1 where name='haha';
# 删除没有值的记录,即null
delete from t1 where name is null;
delete from t1 where name<=>null;
# 清空表
delete from t2;
改
# 修改记录,一定要有定位
update 表名 set 修改字段=值 where 条件字段=值;
eg. update t1 set sex='female' where id=3;
查
# 查看所有的记录
select * from tb_name;
# 查看指定字段的记录
select id,name from t1;
# 子查询
select name,english from t1 where english=(select max(english) from t1);
# 分组统计
select sex,count(sex) from t2 group by sex;
# 只显示前2条记录
select name,chi from t2 order by chi desc limit 2;
聚合函数
sum() # 求和
# 求指定字段的和
select sum(linux) from t2;
# 求指定字段的平均数
select avg(linux) from t2;
# 求和,并且给和设置字段名
select name,chinese+math+english as total from t1;
排序:
# 升序:按照指定字段从小到大的顺序排列
select name,linux from t2 order by linux;
# 降序:按照指定字段从大到小的顺序排列,显示前3名
select name,linux from t2 order by linux desc limit 3;
通配符
# % 匹配任意
select * from t2 where name like '%an';
# _ 匹配单个字符le
select * from t2 where name like 'q_an';
正则表达式:regexp
# 匹配正则表达式
select * from t2 where name regexp 'an$';
运算符:
# 多条件定位(and <---> && or <---> || != =)
update t1 set sex='male' where id=1 and name='huang';
# 多条件匹配 &&<=>and ||<=>or
delete from t1 where id=1 && name='zhao';