select version();

-- 1.学生表
-- S# 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
CREATE TABLE `Student` (
`S#` varchar(10) NOT NULL,
`Sname` varchar(100) NOT NULL,
`Sage` datetime NOT NULL,
`Ssex` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
-- 2.课程表
-- C# --课程编号,Cname 课程名称,T# 教师编号
CREATE TABLE `Course` (
`C#` varchar(10) NOT NULL,
`Cname` varchar(10) NOT NULL,
`T#` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
-- 3.教师表
-- T# 教师编号,Tname 教师姓名
CREATE TABLE `Teacher` (
`T#` varchar(10) NOT NULL,
`Tname` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
-- 4.成绩表
-- S# 学生编号,C# 课程编号,score 分数
CREATE TABLE `SC` (
`S#` varchar(10) NOT NULL,
`C#` varchar(10) NOT NULL,
`score` decimal(18,1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
-- 方式一
SELECT s.`S#` ,s.`C#` ,s.score,s2.`C#` ,s2.score ,s3.Sname ,s3.Sage ,s3.Ssex
from SC s ,SC s2,Student s3 where s.`C#` ='01' and s2.`C#` ='02'
and s.`S#` = s2.`S#`
and s.score > s2.score and s.`S#` = s3.`S#`;
-- 方式二
select A.*,B.`C#`,B.score,C.Sname ,C.Sage ,C.Ssex
from Student C ,(select * from SC where `C#`='01')A
left join(select * from SC where `C#`='02')B
on A.`S#`=B.`S#`
where A.score>B.score and C.`S#` = A.`S#`;

-- 方式一
SELECT * from SC s,SC s2 where s.`C#` ='01' and s2.`C#` ='02' and s.`S#` = s2.`S#`;
-- 方式二
SELECT * from (select * from SC where `C#`='01')A
left join (select * from SC where `C#`='02')B on A.`S#`=B.`S#`
where B.`S#` is not null;

SELECT * FROM (select * from SC where `C#`='01') s
LEFT JOIN (select * from SC where `C#`='02') s2
on s.`S#` = s2.`S#`;
SELECT * from SC s where s.`C#` ='02'
and s.`S#` not in(SELECT s2.`S#` FROM SC s2 where s2.`C#`='01');
select A.`S#`,B.Sname,A.dc from(select `S#`,AVG(score)dc from SC group by `S#`)A
left join Student B on A.`S#`=B.`S#` where A.dc>=60;
select * from Student where `S#` in (select distinct `S#` from SC);
select B.`S#`,B.Sname,A.选课总数,A.总成绩 from
(select `S#`,COUNT(`C#`)选课总数,sum(score)总成绩 from SC group by `S#`)A
right join Student B on A.`S#`=B.`S#`;
select B.`S#`,B.Sname,A.选课总数,A.总成绩 from
(select `S#`,COUNT(`C#`)选课总数,sum(score)总成绩 from SC group by `S#`)A
LEFT join Student B on A.`S#`=B.`S#`;
select COUNT(*)李姓老师数量 from Teacher where Tname like '李%';
select * from Student s
where s.`S#` in ( SELECT s2.`S#` from SC s2
WHERE s2.`C#` IN (SELECT c.`C#` from Course c
WHERE c.`T#` IN (SELECT t.`T#` from Teacher t
WHERE t.Tname='张三')));
select * from Student
where `S#` in(select `S#` from SC group by `S#` having COUNT(`C#`)<3);
select * from Student
where `S#` in(select distinct `S#` from SC
where `C#` in(select `C#` from SC where `S#`='01'));
select * from Student
where `S#` in(select `S#` from SC where `C#` in(select distinct `C#` from SC where `S#`='01') and `S#`<>'01'
group by `S#`
having COUNT(`C#`)>=3)
select * from Student
where `S#` not in(select `S#` from SC
where `C#` in(select c.`C#` from Course c
where c.`T#` in (SELECT t.`T#` from Teacher t where t.Tname='张三')));
select A.`S#`,A.Sname,B.平均成绩 from Student A right join
(select `S#`,AVG(score)平均成绩 from SC where score<60 group by `S#` having COUNT(score)>=2)B
on A.`S#`=B.`S#`;
SELECT * FROM Student s2
where s2.`S#` in(SELECT s.`S#` from SC s
where s.`C#` ='01' and s.score <60 ORDER by score desc);
select`S#`,max(case `C#` when '01' then score else 0 end)'01',
MAX(case `C#` when '02' then score else 0 end)'02',
MAX(case `C#` when '03' then score else 0 end)'03',AVG(score)平均分 from SC
group by `S#` order by 平均分 desc;
select
`C#` ,
count(`S#`) as '选修人数',
max(score) as '最高分',
min(score) as '最低分',
avg(score) as '平均分',
concat( round( 100 * (sum( case when score >= 60 then 1 else 0 end ) / count(score) ), 2 ), '%' ) as '及格率',
concat( round( 100 * (sum( case when score >= 70 and score < 80 then 1 else 0 end ) / count(score) ), 2 ), '%' ) as '中等率',
concat( round( 100 * (sum( case when score >= 80 and score < 90 then 1 else 0 end ) / count(score) ), 2 ), '%' ) as '优良率',
concat( round( 100 * (sum( case when score > 90 then 1 else 0 end ) / count(score) ), 2 ), '%' ) as '优秀率'
from
SC
group by
`C#`
order by
选修人数 desc,
`C#`;
select *,RANK()over(order by score desc)排名 from SC;
select *,DENSE_RANK()over(order by score desc)排名 from SC;
select *,RANK()over(order by 总成绩 desc)排名 from(
select `S#`,SUM(score)总成绩 from SC group by `S#`)A;
select *,DENSE_RANK()over(order by 总成绩 desc)排名 from(
select `S#`,SUM(score)总成绩 from SC group by `S#`)A;
select
b.Cname ,
a.*
from
(select
s.`C#` ,
sum(case when score >=85 and score <100 then 1 else 0 end) as '[85_100]人数' ,
concat(cast(100*sum(case when score >=85 and score <100 then 1 else 0 end)/COUNT(*) as decimal(4,2)) , '%') as '[85_100]占比',
sum(case when score >=70 and score <85 then 1 else 0 end) as '[70_85]人数' ,
concat(cast(100*sum(case when score >=70 and score <85 then 1 else 0 end)/COUNT(*) as decimal(4,2)) , '%') as '[70_85]占比',
sum(case when score >=60 and score <70 then 1 else 0 end) as '[60_70]人数' ,
concat(cast(100*sum(case when score >=60 and score <70 then 1 else 0 end)/COUNT(*) as decimal(4,2)) , '%') as '[60_70]占比',
sum(case when score >=0 and score <60 then 1 else 0 end) as '[0_60]人数' ,
concat(cast(100*sum(case when score >=0 and score <60 then 1 else 0 end)/COUNT(*) as decimal(4,2)) , '%') as '[0_60]占比'
from
SC s
group by s.`C#`) a left join Course b on a.`C#` = b.`C#`;
-- 方法一:
select * from SC a where (select COUNT(*)from SC where `C#`=a.`C#` and score>a.score)<3
order by a.`C#`,a.score desc;
-- 方法二:
select a.`S#`,a.`C#`,a.score from SC a
left join SC b on a.`C#`=b.`C#` and a.score<b.score
group by a.`S#`,a.`C#`,a.score
having COUNT(b.`S#`)<3
order by a.`C#`,a.score desc;
-- 方法三:
-- select * from(select *,rank()over (partition by `C#` order by score desc)A from SC)B where B.A<=3;
select `C#`,COUNT(`S#`)学生数 from SC group by `C#`
select `S#`,Sname from Student
where `S#` in(select `S#` from(select `S#`,COUNT(`C#`)课程数 from SC group by `S#`)A
where A.课程数=2);
select Ssex,COUNT(Ssex)人数 from Student group by Ssex;
select * from Student where Sname like '%风%';
select A.*,B.同名人数 from Student A
left join (select Sname,Ssex,COUNT(*)同名人数 from Student group by Sname,Ssex)B
on A.Sname=B.Sname and A.Ssex=B.Ssex
where B.同名人数>1;
select * from Student where YEAR(Sage)=1990;
select `C#`,AVG(score)平均成绩 from SC group by `C#` order by 平均成绩 desc,`C#`;
select A.`S#`,A.Sname,B.平均成绩 from Student A
left join (select `S#`,AVG(score)平均成绩 from SC group by `S#`)B on A.`S#`=B.`S#`
where B.平均成绩>85;
select B.Sname,A.score from(select * from SC
where score<60 and `C#`=(select `C#` from Course
where Cname='数学'))A
left join Student B on A.`S#`=B.`S#`;
select A.`S#`,B.`C#`,B.score from Student A left join SC B on A.`S#`=B.`S#`;
select A.Sname,D.Cname,D.score from
(select B.*,C.Cname from(select * from SC where score>70)B left join Course C on B.`C#`=C.`C#`)D
left join Student A on D.`S#`=A.`S#`;
SELECT * from Course c where c.`C#` in (select s.`C#` from SC s where score<60);
select A.`S#`,B.Sname from (select * from SC where score>80 and `C#`='01')A
left join Student B on A.`S#`=B.`S#`;
SELECT * from Student st
where st.`S#` =(select s.`S#` from SC s
where s.`C#`=(select c.`C#` from Course c
where c.`T#`=(select t.`T#` from Teacher t
where t.Tname='张三'))
order by s.score desc limit 1);
SELECT * from Student st
where st.`S#` =(select `S#` from(select *,DENSE_RANK()over (order by score desc)A
from SC
where `C#`=(select `C#` from Course
where `T#`=(select `T#` from Teacher where Tname='张三')))B
where B.A=1);
select C.`S#`,max(C.`C#`)`C#`,max(C.score)score from SC C
left join(select `S#`,avg(score)A from SC group by `S#`)B
on C.`S#`=B.`S#`
where C.score=B.A
group by C.`S#`
having COUNT(0)=(select COUNT(0)from SC where `S#`=C.`S#`);
select * from
(select *,ROW_NUMBER()over(partition by `C#` order by score desc)A from SC)B
where B.A<3;
select `C#`,COUNT(`S#`)选修人数 from SC
group by `C#`
having COUNT(`S#`)>5
order by 选修人数 desc,`C#`;
select `S#` from SC
group by `S#`
having COUNT(`C#`)>=2;
select `S#` from SC
group by `S#`
having count(`C#`)=(select distinct COUNT(0)a from Course);
select s.Sname , year(now())-year(s.Sage) as age from Student s
select s.Sname , timestampdiff(year , s.Sage, now()) as age from Student s
select *
from Student s
where WEEKOFYEAR(s.Sage)=WEEKOFYEAR(NOW());
select *
from Student s
where WEEKOFYEAR(s.Sage)=WEEKOFYEAR(NOW())+1;
select *
from Student s
where MONTH(s.Sage)=MONTH(current_date());
select *
from Student s
where MONTH(s.Sage)=MONTH(current_date())+1;
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
嗨~大家好,这里是可莉!今天给大家带来的是7个C语言的经典基础代码~那一起往下看下去把【程序一】打印100到200之间的素数#includeintmain(){ inti; for(i=100;i 【程序二】输出乘法口诀表#includeintmain(){inti;for(i=1;i 【程序三】判断1000年---2000年之间的闰年#includeintmain(){intyear;for(year=1000;year 【程序四】给定两个整形变量的值,将两个值的内容进行交换。这里提供两种方法来进行交换,第一种为创建临时变量来进行交换,第二种是不创建临时变量而直接进行交换。1.创建临时变量来
目录第1题连续问题分析:解法:第2题分组问题分析:解法:第3题间隔连续问题分析:解法:第4题打折日期交叉问题分析:解法:第5题同时在线问题分析:解法:第1题连续问题如下数据为蚂蚁森林中用户领取的减少碳排放量iddtlowcarbon10012021-12-1212310022021-12-124510012021-12-134310012021-12-134510012021-12-132310022021-12-144510012021-12-1423010022021-12-154510012021-12-1523.......找出连续3天及以上减少碳排放量在100以上的用户分析:遇到这类
1.在Python3中,下列关于数学运算结果正确的是:(B)a=10b=3print(a//b)print(a%b)print(a/b)A.3,3,3.3333...B.3,1,3.3333...C.3.3333...,3.3333...,3D.3.3333...,1,3.3333...解析: 在Python中,//表示地板除(向下取整),%表示取余,/表示除(Python2向下取整返回3)2.如下程序Python2会打印多少个数:(D)k=1000whilek>1: print(k)k=k/2A.1000 B.10C.11D.9解析: 按照题意每次循环K/2,直到K值小于等
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
我看到其他人也遇到过类似的问题,但没有一个解决方案对我有用。0.3.14gem与其他gem文件一起存在。我已经完全按照此处指示完成了所有操作:https://github.com/brianmario/mysql2.我仍然得到以下信息。我不知道为什么安装程序指示它找不到include目录,因为我已经检查过它存在。thread.h文件存在,但不在ruby目录中。相反,它在这里:C:\RailsInstaller\DevKit\lib\perl5\5.8\msys\CORE\我正在运行Windows7并尝试在Aptana3中构建我的Rails项目。我的Ruby是1.9.3。$gemin
我已经开始使用mysql2gem。我试图弄清楚一些基本的事情——其中之一是如何明确地执行事务(对于批处理操作,比如多个INSERT/UPDATE查询)。在旧的ruby-mysql中,这是我的方法:client=Mysql.real_connect(...)inserts=["INSERTINTO...","UPDATE..WHEREid=..",#etc]client.autocommit(false)inserts.eachdo|ins|beginclient.query(ins)rescue#handleerrorsorabortentirelyendendclient.commi
我找到了这样的东西:Rails:Howtolistdatabasetables/objectsusingtheRailsconsole?这一行没问题:ActiveRecord::Base.connection.tables并返回所有表但是ActiveRecord::Base.connection.table_structure("users")产生错误:ActiveRecord::Base.connection.table_structure("projects")我认为table_structure不是Postgres方法。如何列出Postgres数据库的Rails控制台中表中的所有
Ruby中防止SQL注入(inject)的好方法是什么? 最佳答案 直接使用ruby?使用准备好的语句:require'mysql'db=Mysql.new('localhost','user','password','database')statement=db.prepare"SELECT*FROMtableWHEREfield=?"statement.execute'value'statement.fetchstatement.close 关于ruby-防止SQL注入(inject
我正在编写一个Rails应用程序,它将监视某些特定数据库的数据质量。为了做到这一点,我需要能够对这些数据库执行直接SQL查询——这当然与用于驱动Rails应用程序模型的数据库不同。简而言之,这意味着我无法使用通过ActiveRecord基础连接的技巧。我需要连接的数据库在设计时是未知的(即:我不能将它们的详细信息放在database.yaml中)。相反,我有一个模型“database_details”,用户将使用它来输入应用程序将在运行时执行查询的数据库的详细信息。因此与这些数据库的连接实际上是动态的,细节仅在运行时解析。 最佳答案