易截截图软件、单文件、免安装、纯绿色、仅160KB

SQL 删除重复数据,只保留1条

if not object_id('Tempdb..#T') is null
    drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 5,N'B',N'B2'
Go
--I、Name相同ID最小的记录(推荐用1,2,3),保留最小一条
方法1:
delete a from #T a where  exists(select 1 from #T where Name=a.Name and ID<a.ID)
方法2:
delete a  from #T a left join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
方法3:
delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)
方法4(注:ID为唯一时可用):
delete a from #T a where ID not in(select min(ID)from #T group by Name)
方法5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)>0
方法6:
delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID)
方法7:
delete a from #T a where ID>any(select ID from #T where Name=a.Name)
select * from #T
生成结果:
/*
ID          Name Memo
----------- ---- ----
1           A    A1
4           B    B1
(2 行受影响)
*/
--II、Name相同ID保留最大的一条记录:
方法1:
delete a from #T a where  exists(select 1 from #T where Name=a.Name and ID>a.ID)
方法2:
delete a  from #T a left join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
方法3:
delete a from #T a where ID not in (select max(ID) from #T where Name=a.Name)
方法4(注:ID为唯一时可用):
delete a from #T a where ID not in(select max(ID)from #T group by Name)
方法5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)>0
方法6:
delete a from #T a where ID<>(select top 1 ID fr


相关文档:

实例名 (SQL Server Express)

使用 Microsoft SQL Server 安装向导的"实例名"页,可指定创建默认实例还是创建 SQL Server Express 命名实例。除非您选择默认实例,否则 SQL Server Express 将始终安装命名实例 (SQLExpress)。此行为与 SQL Server 2005 不同,后者在未选择命名实例的情况下,将始终安装默认实例。
 选项
选项
说明
默认实例 ......

sql中and及or 的执行效率

应用中发现sql中的and及or的执行效率问题
sql语句,为什么把最后的or换成and,查询的就很快,使用的是mssql范例中northwind数据库为例,
select * from Orders a left join [Order Details] b on a.orderid = b.orderid
where a.customerid like '%ics%' or b.productid in (42,72)
追踪了语句的执行方案,发现 ......

SQL Server示例查询

Student(S#,Sname,Sage,Ssex) 学生表
Course(C#,Cname,T#) 课程表
SC(S#,C#,score) 成绩表
Teacher(T#,Tname) 教师表
问题:
1、查询“001”课程比“002”课程成绩高的所有学生的学号;
  select a.S# from (select s#,score from SC where C#='001') a,(select s#,score
  fr ......

1.什么叫SQL注入?如何防止?请举例说明

1.什么叫SQL注入?如何防止?请举例说明
答:SQL注入是常见的利用程序漏洞进行攻击的方法。导致sql注入攻击并非系统造成的,主要是程序中忽略了安全因素,利用sql语言漏洞获得合法身份登陆系统 
例如:
"Select * from users where name='"+uName+"' and pwd='"+uPwd+"' " ......

SQL Server日期操作

1、查询两个时间之间
select * from [tablename] where date between \'value1\' and \'value2\'
 
2、显示最后回复时间
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
 
3、日程安排提前5分钟提醒
select * from 日程安排 w ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号