帮忙!写几个简单的SQL语句

francsharp 2008-02-26 04:33:49
学生成绩表test 字段 id name subject score createtime
1.3天前0点以前创建的记录
2.20分钟前创建的记录
3.查询有3门功课不及格的学生
4.将及格的成绩更新为1,不及格的更新为0
5.删除重复姓名的记录。如
1 A
2 B
3 A
4 A
删除后只剩A和B两条记录。
...全文
134 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
conan304 2008-02-26
  • 打赏
  • 举报
回复
菜鸟试试:

convert(char(10),dateadd(day,-3,getdate()),120)

dateadd(minute,-20,getdate())

--3天前0点以前创建的记录
select * from test
where convert(char(10),createtime,120)<convert(char(10),dateadd(day,-3,getdate()),120)

--20分钟前创建的记录
select * from test
where createtime<dateadd(minute,-20,getdate())

--查询有3门功课不及格的学生
select [name] from test
where score<60
group by [name]
having count(*)>=3

--将及格的成绩更新为1,不及格的更新为0
update test
set score=(case when score>=60 then 1 else 0 end)

/*
5.删除重复姓名的记录。如
1 A
2 B
3 A
4 A
删除后只剩A和B两条记录。
*/
declare @t table([id] int,[name] varchar(2))
insert @t select 1,'A'
union all select 2,'B'
union all select 3,'A'
union all select 4,'A'

delete @t
where [id] not in(
select [id] from @t t
where [id]=(select min([id]) from @t where [name]=t.[name])
)

select * from @t


/*
id name
----------- ----
1 A
2 B
*/
utpcb 2008-02-26
  • 打赏
  • 举报
回复
哈哈大家已经给你解答了
francsharp 2008-02-26
  • 打赏
  • 举报
回复
谢谢,我真是个大菜鸟!结帖!
nzperfect 2008-02-26
  • 打赏
  • 举报
回复
楼上正解
liangCK 2008-02-26
  • 打赏
  • 举报
回复
看看
dawugui 2008-02-26
  • 打赏
  • 举报
回复
学生成绩表test 字段 id name subject score createtime
1.3天前0点以前创建的记录
2.20分钟前创建的记录
3.查询有3门功课不及格的学生
4.将及格的成绩更新为1,不及格的更新为0
5.删除重复姓名的记录。如
1 A
2 B
3 A
4 A
删除后只剩A和B两条记录。

1.3天前0点以前创建的记录 (4天?)
select * from test where datediff(dd,createtime,getdate()) >= 4

2.
select * from test where datediff(mi,createtime,getdate()) >= 20

3.
select name from test where score < 60 group by name having count(*) >= 3

4.update test set score = case when score >= 60 then 1 when score < 60 then 0 end

5.delete test t from test where id not in (select min(id) from test where name = t.name)
5.
shirley_yue 2008-02-26
  • 打赏
  • 举报
回复
2.20分钟前创建的记录
select * from test where createtime<dateadd(minute,-20,getdate())
shirley_yue 2008-02-26
  • 打赏
  • 举报
回复
1.3天前0点以前创建的记录
select * from test where convert(char(10),createtime,120)<convert(char(10),dateadd(day,-3,getdate()),120)
liangCK 2008-02-26
  • 打赏
  • 举报
回复
不做作业很多年.

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧