您好,欢迎访问一九零五行业门户网

MsSqlServer语句_MySQL

--如果 成绩>100 优
--如果成绩>90 良
select * from tblscore
select 英语成绩=
(case when tenglish>90 then '良' when tenglish>100 then'优' end),数学成绩=(case when tmath>90 then '良' when tmath>100 then'优' end) from tblscore
--第二个练习 1 2 3
select * from user5
select 等级=(case when [level]=1 then'骨灰' when [level]=2 then'菜鸟' when [level]=3then '大神' end) from user5
--第三个练习
--6000 5500 4500
select * from myorders
select 销售员,销售总金额=sum(销售数量*销售价格),称号=(
case
when sum(销售价格*销售数量)>6000
then '金牌'
when sum(销售价格*销售数量)>5500
then '银牌'
when sum(销售价格*销售数量)>4500
then '铜牌'
else
'通牌'
end
) from myorders
group by 销售员
--收入 支出
select * from test
select number,收入=(
case
when amount>0
then amount
when amount then 0
end
),支出=(case
when amount
then abs(amount)
when amount>0
then 0
end) from test
--查询所有的英语成绩 并英语的成绩>90 --子查询做
select * from ( select tenglish from tblscore ) as t where t.tenglish>90
--查询性别是男 年龄在20岁以上的
select * from( select * from tblstudent where tsgender='男') as t where t.tsage>20
--1.查询出班级中所有24岁的男生(子查询)
select * from ( select * from tblstudent where tsgender='男') as t where tsage=24
--2.查询出高一三班和高二二班的所有学生(子查询)
select * from tblstudent where tsclassid in(
select tclassid from tblclass where tclassname='高一一班' or tclassname='高二二班')
--2.查出黑马一期和黑马二期的所有学生
use myitcast
select * from student
select * from tblclass
select * from student where tclassid in(select tclassid from tblclass where tclassname='黑马一期' or tclassname='黑马二期' )
--3.查询出的总人数,男同学多少人,数学平均成绩(子查询)
select 总人数=(select count(*)from student) ,男同学多少人=(select count(*) from student where tsgender=1),数学平均成绩=(select avg(tblscore.tsmath) from tblscore)
--9条到16条的数据
select * from student
select top 8 * from student where tsid not in(select top 8 tsid from student) --
--16 到 26
select top 8 * from student where tsid not in( select top 15 tsid from student)
select * from student
use nononodeleteimportant
select * from tblstudent
--每页三条 查第五页的
select * from (
select * ,编号=row_number() over(order by tsid) from tblstudent ) as newtbl where newtbl.编号 between (5-1)*3+1 and 5*3
--每页9条数据 查询13页的
select * from (
select 编号=row_number() over(order by tsid),* from tblstudent) as t where t.编号 between (13-1)*9+1 and 13*9
select tmath,名次= row_number() over(order by tmath) from tblscore
select tmath,名次=rank() over(order by tmath) from tblscore --rank相同成绩的排名相同
select * from myorders
select 商品名称,行号=row_number() over(partition by 商品名称 order by id) from myorders --partition by 分区
--销售员的销售总金额
select * from myorders
select 销售员,销售总金额=sum(销售数量*销售价格) from myorders
group by 销售员
--2.统计每个销售员(订单)的销售金额占总销售金额的百分比。
select * ,销售数量*销售价格,
百分比=销售数量*销售价格*1.0/sum(销售数量*销售价格) over(partition by 销售员 )*100
from myorders
--链接查询
--查询这个学生的时候能不能把这个学生所在的班级的名字也显示出来
select tblstudent.tsname,tblstudent.tsage,tblstudent.tsgender,tblclass.tclassname from tblstudent
inner join tblclass
on tblstudent.tsclassid=tblclass.tclassid
--查询这个学生在哪个班级,他(她)的考试成绩
select tblstudent.tsname,tblstudent.tsgender,tblclass.tclassname,tblscore.tenglish,tblscore.tmath from tblstudent
inner join tblclass
on tblstudent.tsclassid=tblclass.tclassid
inner join tblscore
on tblstudent.tsid=tblscore.tsid
--创建视图
create view vw_stu_cla_sco_newview
as
select tblstudent.tsname,tblstudent.tsgender,tblclass.tclassname,tblscore.tenglish,tblscore.tmath from tblstudent
inner join tblclass
on tblstudent.tsclassid=tblclass.tclassid
inner join tblscore
on tblstudent.tsid=tblscore.tsid
--
select * from vw_stu_cla_sco_newview --查询视图
drop view vw_stu_cla_sco_newview --删除视图
--查询年龄超过20岁的学生的姓名、年龄及所在班级
select tblstudent.tsname,tblstudent.tsage,tblclass.tclassname from tblstudent
inner join
tblclass
on
tblstudent.tsclassid=tblclass.tclassid
inner join
tblscore
on
tblstudent.tsid=tblscore.tsid
where tblstudent.tsage>20
--
--查询所有学生(参加及未参加考试的都算)及成绩
select * from tblstudent
inner join tblscore
on tblstudent.tsclassid=tblscore.tsid --参加考试的学生
select tblstudent.tsname, tblscore.tmath,tblscore.tenglish from tblstudent
left join tblscore
on tblstudent.tsclassid=tblscore.tsid --参加考试的学生和没参加考试的学生
select tblstudent.tsname, tblscore.tmath,tblscore.tenglish from tblstudent
left join tblscore
on tblstudent.tsclassid=tblscore.tsid
where tblscore.tsid is null --没参加考试的学生
--查询所有参加考试的,english分数不为null学生姓名、年龄及成绩
select tblstudent.tsname, tblscore.tmath,tblscore.tenglish from tblstudent
inner join tblscore
on tblstudent.tsclassid=tblscore.tsid
where tblscore.tenglish is not null --参加考试的学生,英语成绩不为null
--练习3:查询所有学生(参加和未参加考试)的学生姓名、年龄、成绩,如果没有参加考试显示缺考,如果小于english&math60分显示不及格
use nononodeleteimportant
select tblstudent.tsname,tblstudent.tsage,英语成绩=(case
when tenglish is null
then '缺考'
else
convert(nvarchar,tenglish)
end),数学成绩=(case
when tmath is null
then '缺考'
else
convert(nvarchar,tmath)
end ),是否及格=(case when tenglish>60 and tmath>60 then '及格'
else '不及格'
end) from tblstudent left join
tblscore on tblstudent.tsid=tblscore.tsid
select * from tblarea
select t.areaid,t.areaname,t1.areaname from tblarea as t inner join tblarea as t1 on t.areapid=t1.areaid
--声明变量
declare @number int ;
set @number=30;
print @number
select @number
if(@number=30)
begin
print '好帅'
end
else
begin
select '真心恶心'
end
declare @avg int =0
set @avg=(select avg(tmath) from tblscore)
if(@avg>60)
begin
select top 3 * from tblscore order by tmath desc
end
else
begin
select top 3 * from tblscore order by tmath asc
end
其它类似信息

推荐信息