sql语句快速入门基础篇

一:增删改查

 

 

insert into + 表名(列名字段)value(列名对应值)

如:insert into student(id,name,sex)value('1','小白','男');

delete from +表名 where +条件;

如:delete  from bm_act_spot_check_audit where id = '2612' ;

update +表名 set 列名字段 改为 值 where +条件;

如:update bm_act_spot_check_audit set `city_id`='110100' where `id`='2612' ;

select * from +表名 where + 条件

如:select * from bm_act_spot_check_audit where `rider_name`='测试'  ;

select  列名字段 from +表名 where + 条件 即 只查询 该列数据

如:select act_type from bm_act_spot_check_audit where `rider_name`='测试'  ;

二:常用函数

求平均值 avg(列名字段)

select avg(plan_id) from bm_act_spot_check_audit where `rider_name`='测试' and `act_type`='2' ;

求最小绝对值 min(列名字段)

select min(plan_id) from bm_act_spot_check_audit where `rider_name`='测试' and `act_type`='2' ;

求最大绝对值 max(列名字段)

select max(plan_id) from bm_act_spot_check_audit where `rider_name`='测试' and `act_type`='2' ;

求和 sum(列名字段)

select sum(plan_id) from bm_act_spot_check_audit where `rider_name`='测试' and `act_type`='2' ;

求计算条数 count(*)

select count(*) from `bm_act_spot_check_audit` where `rider_name`='测试' and `act_type`>1;

三:排序查询

升序查询 order by +列名字段 +asc

如:select * from `bm_act_spot_check_audit` where `rider_name`='测试' order by `act_type` ASC ;

降序查询 order by +列名字段 +desc

如:select * from `bm_act_spot_check_audit` where `rider_name`='测试' order by `act_type` desc ;

 

四:分组查询

group by 列名字段

如:select * from bm_act_spot_check_audit where `rider_name`='测试' group by act_type  ;

带条件分组

group by 列名字段 having +条件

如:select * from bm_act_spot_check_audit where `rider_name`='测试' group by act_type having `act_type`>1  ;

 

五:模糊查询

%a  即字符串前不限长度,只有后边包含 a即符合查询条件

如:select * from bm_act_spot_check_audit where `rider_name` like '%测试' ;

_a 即查询字符a前只有一个字符的 结果符合查询条件,如 _ab _ac _ade 

如:select * from bm_act_spot_check_audit where `rider_name` like '_测试' ;

[abc]d 即 查询出结果为 ad bd cd 符合查询结果

如:select * from bm_act_spot_check_audit where `rider_name` like 'TEST[1-9]' ;

[^ABC]D 即 查询出非AD BD CD 的符合条件的结果

如:select * from bm_act_spot_check_audit where `rider_name` like '测试[^4-6]' ;

 

六:分页

limit 0,5; 即 展示1到第5行 

如:select * from bm_act_spot_check_audit where `rider_mobile`='14000000000' limit 0,5 ;
 

七:左连接查询/右连接查询/内连接查询/外连接查询

假设 有 学生表student

idname
1张三
2李四
3王五
4婉婉

 

成绩表achievement

idasidenglish
1190
2178
3399
4470
55100

左连接查询  left join ... on 

即 把左边表的数据全拿出来,右边没有的 为空,

如:查询出每个学生的英语成绩

select s.name,a.english from student s left join achievement a on s.id=a.asid;

其结果如下

nameenglish
张三90
张三78
李四 
王五99
婉婉70

 

右连接查询 right join ... on

即把右边的表数据全拿出来,左边表的数据若没有,可以为空,同样,若左边的数据多余的也不展示

如:查询参加英语考试的每个学生的成绩

select s.name,a.english from student s right join achievement a on s.id=a.asid;

其结果如下

nameenglish
张三90
张三78
王五99
婉婉70

内连接查询 inner join ... on

即 两表都有的数据展示,其他数据不展示

如:select s.name,a.english from student s inner join achievement a on s.id=a.asid;

其结果如下

nameenglish
张三90
张三78
王五99
婉婉70

全连接查询  full outer join  .... on

即 两张表的数据全部展示,无对应的数据展示空

如:select s.name,a.english from student s full outer join achievement a on s.id=a.asid ;

其结果如下

nameenglish
张三90
张三78
李四 
王五99
婉婉70
 100

八:其他英文字段说明

and  相当于  必须满足

如:select * from bm_act_spot_check_audit where `rider_name` ='测试' and `act_type`='2';

or  相当于  不需要都满足,满足任意一个条件即可

如:select * from bm_act_spot_check_audit where `rider_name` ='测试' or `act_type`='2';

  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值