请教一个SQL,关于数据库录入结果比对功能

CQP 2008-05-28 11:16:53
请教一个SQL,关于数据库录入结果比对功能

现小王、小李录入二条数据,分别如下
字段名
序号姓名 分数1 分数2 录入人员
1 张三 7 10 小王
2 李四 10 3 小王
3 张三 10 10 小李
4 李四 10 3 小李

我现在要写个SQL语句,查出小王与小李录入的数据不一致的是哪条数据

这样的SQL要怎么写,从上面的结果可以看出张三的分数两个人录入不一致??

...全文
132 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wpeng8218 2008-05-28
  • 打赏
  • 举报
回复
declare @t table(id int identity(1,1),[name] varchar(10),code1 int,code2 int, entryName varchar(10))
insert into @t([name],code1,code2,entryName)
select '张三',7,10,'小王'
union all select '李四',10,3,'小王'
union all select '张三',10,10,'小李'
union all select '李四',10,3,'小李'


select * from (
select * from @t where entryName='小王'
) t1,(
select * from @t where entryName='小李'
) t2
where t1.[name]=t2.[name] and checksum(t1.code1,t1.code2)<>checksum(t2.code1,t2.code2)
dawugui 2008-05-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 CQP 的帖子:]
请教一个SQL,关于数据库录入结果比对功能

现小王、小李录入二条数据,分别如下
字段名
序号姓名 分数1 分数2 录入人员
1 张三 7 10 小王
2 李四 10 3 小王
3 张三 10 10 小李
4 李四 10 3 小李

我现在要写个SQL语句,查出小王与小李录入的数据不一致的是哪条数据

这样的SQL要怎么写,从上面的结果可以看出张三的分数两个人录入不一致??
[/Quote]
-前提:表中不能有text、ntext、image、cursor 数据类型的字段。
select m.* from
(select * from tb where 录入人员 = '小王') m
checksum(*) not in
(select * from tb where 录入人员 = '小李') n
utpcb 2008-05-28
  • 打赏
  • 举报
回复
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (序号 int,姓名 varchar(11),分数1 int,分数2 int,录入人员 varchar(11))
insert into #T
select 1,'张三',7,10,'小王' union all
select 2,'李四',10,3,'小王' union all
select 3,'张三',10,10,'小李' union all
select 4,'李四',10,3,'小李'

--> 2005
select 姓名,分数1,分数2 from #T where 录入人员='小王'
except
select 姓名,分数1,分数2 from #T where 录入人员='小李'

/*
姓名 分数1 分数2
----------- ----------- -----------
张三 7 10
*/

--> 2000
select * from #T as t where 录入人员='小王' and not exists (select 1 from #T where 录入人员='小李' and 姓名=t.姓名 and 分数1=t.分数1 and 分数2=t.分数2)
/*
序号 姓名 分数1 分数2 录入人员
----------- ----------- ----------- ----------- -----------
1 张三 7 10 小王
*/

撩天槌 2008-05-28
  • 打赏
  • 举报
回复
if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([序号] int,[姓名] nvarchar(2),[分数1] int,[分数2] int,[录入人员] nvarchar(2))
Insert #T
select 1,'张三',7,10,'小王' union all
select 2,'李四',10,3,'小王' union all
select 3,'张三',10,10,'小李' union all
select 4,'李四',10,3,'小李'
Go



select *from #t a,#t b
where a.录入人员='小王' and b.录入人员='小李'
and a.姓名=b.姓名 and (a.分数1<>b.分数1 or a.分数2<>b.分数2)
kuangdp 2008-05-28
  • 打赏
  • 举报
回复
加一个字段,保存他们录入的数据的报告号!
然后用存储过程比较他们相同报告号的录入数据的分数字段等,不相同的,可以返回报告ID
hery2002 2008-05-28
  • 打赏
  • 举报
回复
--> 测试数据

declare @tb table ([seq] int,[cname] nvarchar(2),[score] int,[score2] int,[op] nvarchar(2))
Insert into @tb
select 1,'张三',7,10,'小王' union all
select 2,'李四',10,3,'小王' union all
select 3,'张三',10,10,'小李' union all
select 4,'李四',10,3,'小李'

Select * from @tb a
left join @tb b on a.[cname] = b.[cname] and b.[op] ='小李'
where a.[op] ='小王' and ((a.[score] != b.[score]) or (a.[score2] != b.[score2]))
/*
seq cname score score2 op seq cname score score2 op
----------- ----- ----------- ----------- ---- ----------- ----- ----------- ----------- ----
1 张三 7 10 小王 3 张三 10 10 小李

(1 row(s) affected)
*/
Limpire 2008-05-28
  • 打赏
  • 举报
回复
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (序号 int,姓名 varchar(11),分数1 int,分数2 int,录入人员 varchar(11))
insert into #T
select 1,'张三',7,10,'小王' union all
select 2,'李四',10,3,'小王' union all
select 3,'张三',10,10,'小李' union all
select 4,'李四',10,3,'小李'

--> 2005
select 姓名,分数1,分数2 from #T where 录入人员='小王'
except
select 姓名,分数1,分数2 from #T where 录入人员='小李'

/*
姓名 分数1 分数2
----------- ----------- -----------
张三 7 10
*/

--> 2000
select * from #T as t where 录入人员='小王' and not exists (select 1 from #T where 录入人员='小李' and 姓名=t.姓名 and 分数1=t.分数1 and 分数2=t.分数2)
/*
序号 姓名 分数1 分数2 录入人员
----------- ----------- ----------- ----------- -----------
1 张三 7 10 小王
*/
jinjazz 2008-05-28
  • 打赏
  • 举报
回复
select * from (select * from tb where 录入人员='小王')a, (select * from tb where 录入人员='小李')b
where a.姓名=b.姓名 and checksum(a.分数1 ,a.分数2)<>checksum(b.分数1 ,b.分数2)
烈火焚身 2008-05-28
  • 打赏
  • 举报
回复
学习
友情帮顶!!!
ChinaJiaBing 2008-05-28
  • 打赏
  • 举报
回复

select a.* from a not exists (select * from a group by 录入人员)
zhiguo2008 2008-05-28
  • 打赏
  • 举报
回复
create table # (序号 int ,姓名 nvarchar(100),分数1 int,分数2 int,录入人 nvarchar(100))
insert into # select 1,'张三',7,10,'小王' union select 2,'李四',10,3,'小王'
union
select 3,'张三',10,3,'小李' union select 4,'李四',10,3,'小李'

select * from #
select * from # A where 录入人='小王' and exists(select * from # B where B.录入人='小李' and A.姓名=B.姓名 and (A.分数1!=B.分数1 or A.分数2!=B.分数2))


Result:
序号 姓名 分数1 分数2 录入人
----------- ---------------------------------------------------------------------------------------------------- ----------- ----------- ----------------------------------------------------------------------------------------------------
1 张三 7 10 小王
2 李四 10 3 小王
3 张三 10 3 小李
4 李四 10 3 小李

(所影响的行数为 4 行)

序号 姓名 分数1 分数2 录入人
----------- ---------------------------------------------------------------------------------------------------- ----------- ----------- ----------------------------------------------------------------------------------------------------
1 张三 7 10 小王

(所影响的行数为 1 行)

ChinaJiaBing 2008-05-28
  • 打赏
  • 举报
回复
序号姓名 分数1 分数2 录入人员
1 张三 7 10 小王
2 李四 10 3 小王
3 张三 10 10 小李
4 李四 10 3 小李

我现在要写个SQL语句,查出小王与小李录入的数据不一致的是哪条数据
decimal @a table
( 序号 int,
姓名 nvarchar(6),
分数1 decimal(5,1),
分数2 decimal(5,1),
录入人员 nvarchar nvarchar(6)
)
insert into @a select 1,'张三',7,10,'小王'
union all select 2,'李四',10,3,'小王'
union all select 3,'张三',10,10 ,'小李'
union all select 4,'李四',10,3,'小李'
select a.* from @a a not exists(select a.* from a group by a.录入人员)

34,588

社区成员

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

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