elect distinct name from student
--查看有几个性别
select distinct sex from student
?
--in in后面的括号中是用来判断是否有此(条件)的,可以看做是元素
--查看姓名为李狗蛋和铃铛的所有信息
select * from student where name in('李狗蛋','铃铛')
?
--引号里面括号外加下划线,意思是选择里面任意一个值,不常用
select * from student where name like'_[李狗蛋,铃铛]'
?
?
?
--聚合函数:sum avg max min count
--求所有分数的总和
select SUM(score) as 总和 from student
?
--求所有分数的平均分
select AVG(score) as 平均分 from student
?
--查看最高分
select MAX(score) as 最高分 from student
--查看最低分
select MIN(score) as 最低分 from student
?
--查看总人数
select COUNT(*) as 总人数 from student
--查看叫李四的有几个
select COUNT(*) as 数量 from student where name ='李四'
?
--group by 分组
--按照男女来分组
select sex from student group by sex
--按照男女来分组,分组之后求平均分
select sex as 性别 , AVG(score) as 平均分 from student group by sex
--分别查看男女的数量
select sex as 性别, COUNT(*) as 人数 from student group by sex
--分别查看男女,并且分数在70以上的人的个数
select sex as 性别, COUNT(*) as 人数 from student where score >=70 group by sex
--分别查看男女,并且分数在70以上的并且人数超过3个的组
select sex as 性别, COUNT(*) as 人数 from student where score >=70 group by sex having COUNT(*)>3
?
?
--按照男女来分组,分组之后求平均分,并且平均分>70
select sex as 性别 , AVG(score) as 平均分 from student group by sex having AVG(score)>70
?
?
?
3、实例
?
create table cangku
(
code int,
name varchar(50),
zong int,
price decimal(18,2)
)
go
insert into cangku values(1,'苹果',30,2.9)
insert into cangku values(2,'梨',30,2)
insert into cangku values(3,'西瓜',37,1.9)
insert into cangku values(4,'馒头',30,1)
insert into cangku values(5,'猪肉',20,5)
insert into cangku values(6,'茄子',45,3.6)
insert into cangku values(7,'黄瓜',60,2.4)
insert into cangku values(8,'白菜',30,0.8)
insert into cangku values(9,'哈密瓜',70,3)
insert into cangku values(10,'南瓜',30,1.89)
--卖掉了第3种产品3个
update cangku set zong=34 where code=3
--卖掉第4种产品7个
update cangku set zong=23 where code=4
?
--卖掉第5种产品5个
update cangku set zong=15 where code=5
?
--查看现在货物库存最少的商品全部信息
select top 1* from cangku order by zong?
--货物最少的商品进货补齐30件
update cangku set zong=30 from cangku where code=5
--现在第3种,第4种,第5种产品都涨价一块
update cangku set price=price+1 from cangku where code in(3,4,5)
--第6种第7种第8种统统减价一块
update cangku set price=price-1 from cangku where code in(6,7,8)
?
--卖掉了第6种产品3个
update cangku set zong= zong-3 from cangku where code=6
--卖掉第7种产品3个
update cangku set zong= zong-3 from cangku where code=7
--卖掉第8种产品9个
update cangku set zong= zong-9 from cangku where code=8
--查看现在货物库存最少的商品全部信息
select top 1* from cangku order by zong
--货物最少的商品进货补齐30件
update cangku set zong=30 from cangku where code=5
select*from cangku