一个软件公司的笔试题目,请各位大侠指点,答对者给200分 (200分)

  • 主题发起人 linyongdao
  • 开始时间
L

linyongdao

Unregistered / Unconfirmed
GUEST, unregistred user!
题目:有两个相同的表T1,T2,表结构如下:
字段名 类型 长度 是否为主键
Pa, 字符 10 主键
Pb, 字符 10 主键
Pc, 字符 10 主键
d, 字符 10
e 字符 10

T1和T2里有相同的数据, 现要求用一条Sql语句把T2中的数据导入T1(只能导入T1中不存在的数据)
注意这里的Pa pB Pc都是主键
 
我用了如下方法
insert T1 (pa,pb,pc, d,e )
select pa,pb,pc, d,e
from t2
where pa+pb+pc not in ( select pa+pb+pc from t1 )
但在SQL SERVER 查询分析器 上执行时提示:
‘无法解决 not equal to 操作的排序规则冲突。’
 
insert T1 SELECT T2.* FROM T2 WHERE T2.PA NOT IN (SELECT PA FROM T1)
 
insert into t1(pa,pb,pc,d,e)
select * from t2 where not exists(select * from t2,t1 where t1.pa=t2.pa
and t2.pb=t1.pb and t1.pc=t2.pc)
 
insert into t1(pa,pb,pc,d,e)
select * from t2 where not exists(select * from t2,t1 where t1.pa=t2.pa
and t2.pb=t1.pb and t1.pc=t2.pc)
 
insert into t1(pa,pb,pc,d,e)
select t2.* from t2,t1 where t1.pa<>t2.pa
or t2.pb<>t1.pb or t1.pc<>t2.pc
 
insert into t1(pa,pb,pc,d,e)
select pa,pb,pc,d,e from t2 where pa+pb+pc not in(select pa+pb+pc from t1)
可以通過
 
同意ugvanxk,nzfsoft的答案。
insert into t1
select * from t2 where not exists(select t2.* from t2,t1 where t1.pa=t2.pa
and t2.pb=t1.pb and t1.pc=t2.pc)

 
试了各位大侠的语句,但在SQL SERVER 查询分析器 上执行时还是提示:
‘无法解决 not equal to 操作的排序规则冲突。’
Pa pB Pc都是主键所以会出现上面的错误,如果Pa pB Pc都不是主键是可以通过
 
只判断一个字段值不同就可以了。
insert into t1(pa,pb,pc,d,e)
select * from t2 where not exists(select * from t2,t1 where t1.pa=t2.pa)
 
insert T1 (pa,pb,pc, d,e )
select pa,pb,pc, d,e
from t2
where pa,pb,pc in ( select pa,pb,pc from t1
mimus select pa,pb,pc from t2)
原则上not in是最不优化的结构
应该用in和minus结合完成。
btw:你的写法有逻辑错误pa+pb+pc第一会导致数值加减错误,
字符也会导致连接逻辑相同错误,a,bb,c和ab,b,c一样
同理用pa||pb||pc也一样
 
我只会用临时表。。。
select * from t2 where t1.pa<>t2.pa

然后再插入。。。。当我没说。
 
to: gophie
insert T1 (pa,pb,pc, d,e )
select pa,pb,pc, d,e
from t2
where pa,pb,pc in ( select pa,pb,pc from t1
mimus select pa,pb,pc from t2)
原则上not in是最不优化的结构
应该用in和minus结合完成。

请问 mimus 和 minus 哪个是正确的?我没有用过,能解释一下它的意思和用法吗?

 
gophie:
where pa,pb,pc in ( select pa,pb,pc from t1
mimus select pa,pb,pc from t2)
会提示: ',' 附近有语法错误。

 
insert into t1
select * from t2 where pa+pb+pc not in(select pa+pb+pc from t1)
测试通过,没有问题的。(Pa pB Pc都是主键)

to: gophie
insert T1 (pa,pb,pc, d,e )
select pa,pb,pc, d,e
from t2
where pa,pb,pc in ( select pa,pb,pc from t1
mimus select pa,pb,pc from t2)
会提示有语法错误:
Server: Msg 170, Level 15, State 1, Line 13
Line 13: Incorrect syntax near ','.
Server: Msg 156, Level 15, State 1, Line 14
Incorrect syntax near the keyword 'select'.
Server: Msg 170, Level 15, State 1, Line 14
Line 14: Incorrect syntax near ')'.
同意你的以下观点:
你的写法有逻辑错误pa+pb+pc第一会导致数值加减错误,
字符也会导致连接逻辑相同错误,a,bb,c和ab,b,c一样
同理用pa||pb||pc也一样
 
delphi中不能使用minus
 
试试
insert into T1
select pa,pb,pc, d,e
from t2
where pa,pb,pc in ( select pa,pb,pc from t2
minus select pa,pb,pc from t1)
这种写法在oracle805以上能通过。
如果不行,也许sql server不支持这种写法。
不好意思。
不过意思就是这个意思啦,
not in 可以用in和minus结合的写法替代,而且更优化
 
实现方法:
表T1、T2结构如下
========================
id1 int 主健
id2 int 主健
id3 int 主健
V1 varchar
V2 varchar
=======================
insert t1 select * from t2 where not exists
(select * from t1 where t1.id1=t2.id1 and t1.id2=t2.id2 and t1.id3=t2.id3 and T1.V1=T2.v1 and T1.v2=T2.v2)
 
insert t1 select * from t2 where t2.pa+t2.pb+t2.pc not in (select t1.pa+t1.pb+t1.pc from t1)
 
楼上的兄弟们有没有考虑如下情况:
pa+pb+pc第一会导致数值加减错误,
字符也会导致连接逻辑相同错误,a,bb,c和ab,b,c一样
同理用pa||pb||pc也一样
那么你的insert 语句执行的结果是否正确呢?
如果t1表中有如下记录
字段 pa pb pc d e
记录 a bb c d1 e1
如果t2表中有如下记录
字段 pa pb pc d e
记录 ab b c d2 e2
这条记录在t1中是没有的,那么还会insert 进去吗?
 
顶部