sql

select t.品牌,sum(a)

from (select distinct day(dt_t3) a , t3.用户浏览ID, t2.品牌
from  t3
left join t1
on t3. 商品编码t3 = t1. 商品编码t1  left join t2 on t1. 商品编码t1 = t2. 商品编码t2
where year(dt_t3) = '2019'
) t 
group by t.品牌;







select distinct day(dt_t3) , t3.用户浏览ID,t1.品牌
from  t3
left join t1
on t3. 商品编码t3 = t1. 商品编码t1
where year(dt_t3);



select* from student;
select * from record_time;
select * from sc;

create table  record_time(
sno varchar(10) ,
record_time date);

insert into record_time values ('9512101','2019/1/5');
insert into record_time values ('9512102','2019/6/5');
insert into record_time values ('9512103','2019/1/5');
insert into record_time values ('9512104','2019/10/5');


# 查询近六个月没有违纪的学生
create table b as
select * , sum(grade) 总分
from student left join sc on student.sno1 = sc.sno
where sno1 not in (
select sno from record_time where  record_time BETWEEN DATE_SUB(NOW(), INTERVAL 6 MONTH) AND NOW())
group by sno1;

select * from b;


# 查询六个月中没有违纪且总分排名在各年级前十的
select sno1,sname,ssex,sdept,总分,rank
from(
select b.*,if(@p = b.sdept,@r := @r+1,@r := 1) rank,@p := b.sdept
from 
b,(select @p :=null,@r := 0) r
order by b.sdept,b.总分 desc) m
where rank<=3
; 







select sno1,sname,ssex,sdept,总分,rank
from(
	select b.*,if(@p = b.sdept,@r := @r+1,@r := 1) rank,@p := b.sdept
from 
	(select * , sum(grade) 总分
		from student left join sc on student.sno1 = sc.sno
			where sno1 not in (
					select sno from record_time where  record_time BETWEEN DATE_SUB(NOW(), INTERVAL 6 MONTH) AND NOW())
					group by sno1) b,(select @p :=null,@r := 0) r
					order by b.sdept,b.总分 desc) m
where rank<=3
; 




select m.stu_id,class,总分,rank
from(
	select b.*,if(@p = b.class,@r := @r+1,@r := 1) rank,@p := b.class
from 
	(select * , sum(score) 总分
		from stu_table m left join score_table s on m.stu_id = s.stu_id
			where m.stu_id not in (
					select stu_id from record_time where  record_time BETWEEN DATE_SUB(NOW(), INTERVAL 6 MONTH) AND NOW())
					group by m.stu_id) b,(select @p :=null,@r := 0) r
					order by b.calss,b.总分 desc) m
where rank<=10
; 




select class , score,rank
from 
(
select a.*,if(@p = a.class,@r:=@r+1,@r:=1) rank,@p :=a.class
from student22 a ,(select @p:=null,@r:=0) r
order by a.class asc,a.score desc) z
where rank<=3;





create table student22(
	id varchar(20),-- 编号
	class varchar(20),-- 年级
	score int-- 分数
);



insert student22 values('1','一年级',82);
insert student22 values('2','一年级',95);
insert student22 values('3','一年级',82);
insert student22 values('4','一年级',40);
insert student22 values('5','一年级',20);
insert student22 values('6','二年级',95);
insert student22 values('7','二年级',40);
insert student22 values('8','二年级',3);
insert student22 values('9','二年级',60);
insert student22 values('10','二年级',10);
insert student22 values('11','三年级',70);
insert student22 values('12','三年级',60);
insert student22 values('13','三年级',40);
insert student22 values('14','三年级',90);
insert student22 values('15','三年级',20);


select a.class,a.score from student22 a where (select count(*) from student22 where a.class=class and a.score 
 

  

你可能感兴趣的:(sql)