SQL语句(二)

SQL 语句

1.sql的where各种条件

select * from rxdata where name = wjw; //查询name为wjw的数据
select * from rxdata where name = wjw and age = 24;//查询name为wjw并且age为24的数据
select * from rxdata where name = wjw or age = 24;//查询name为wjw或age为24的数据
select*  from rxdata where name like '%j%'; //查询name存在j的数据w
select*  from rxdata where name like 'w%'; //查询name存在开头为w的数据
select*  from rxdata where name like '%'w; //查询name存在结尾为w的数据
select*  from rxdata where name like '_j%'; //查询name存在第二个为j的数据
select*  from rxdata where name not like 'w%'; //查询name存在开头不为w的数据

2.union合并

先创建a,b表,并插入数据

create table a(id int, aname varchar(200),age int );
create table b(id int, bname varchar(200),address varchar(200));

union 和union all

select id,aname from a  union select id,bname from b;

在这里插入图片描述

select id,aname from a  union all select id,bname from b;

在这里插入图片描述

3.join语句

左连接 left join

select a.*,b.* from a left join b on a.id=b.id;
idanameageidbnameaddress
1wjw1241wjw1hf
3wjw3263wjw3bj
2wjw225nullnullnull

右连接 right join

select a.*,b.* from a right join b on a.id=b.id;
idanameageidbnameaddress
1wjw1241wjw1hf
3wjw3263wjw3bj
nullnullnull5wjw5sh

内连接 inner join

select a.*,b.* from a inner join b on a.id=b.id;
idanameageidbnameaddress
1wjw1241wjw1hf
3wjw3263wjw3bj

4.统计语句

select * from a ;
idanameage
1wjw124
2wjw225
3wjw326
4wjw123
5wjw222
//sum求和
select sum(age) as agesum from a;  #数值求和   
agesum
120
//count统计数据条数
select count(*)  from a;  #数值求和 求条数  1+1+1=3
count(*)
5

注意点
a.count(*)少用 count(id) count(0)
b.算age数据多少 sum(age)
c.count(distinct id) 去重

#求各个名称下的sum(age)
select aname,sum(age) from  a  group by aname;
anamesum(age)
wjw147
wjw247
wjw326

#求age>22的各个名称的年龄和

select t.aname,sum(t.age) from (select * from a where age > 22) as t group by t.aname; //子查询

select aname,sum(age) from a where age>22 group by aname;
anamesum(age)
wjw147
wjw225
wjw326
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值