作者cutekid (可爱小孩子)
看板Database
标题Re: [SQL ] 栏位比对
时间Mon Oct 14 14:11:49 2013
※ 引述《pokl123 (00)》之铭言:
: 有两张table A,B
: [问题]
: 当table A的I1~I4任一栏位符合 table B的k栏位时,
: 且table A的I栏位值不在 table B的S跟F栏位之间,
: 就把table A的result设为Y
: table_A
: I I1 I2 I3 I4 result
: 2555 2749 2554
: 71542 2851 42789 78841 25000
: table_B
: k item S F
: 2554 1 1760 1766
: 2554 2 1790 1796
: 2554 3 2553 2559
: 2554 4 2595 2599
: 2851 1 01580 01596
: 2851 2 01790 01796
: 2851 3 71535 71540
: 2851 4 71550 71555
: 2851 5 71600 71611
: 我目前的想法
: update A
: set result='Y'
: from A,B
: where
: (
: A.I1=B.k AND (I not between B.S and B.F) or
: A.I2=B.k AND (I not between B.S and B.F) or
: A.I3=B.k AND (I not between B.S and B.F) or
: A.I4=B.k AND (I not between B.S and B.F)
: )
updata A
set result = 'Y'
from A,B
where (
(A.I1 = B.k or A.I2 = B.k or A.I3 = B.k or A.I4 = B.k)
and not exists (
select k
from B tmp
where tmp.k = b.k and A.I between tmp.S and tmp.F
)
)
: 但是,这个写法会没办法比较每一笔资料,
: 也就是当比完下面这笔资料後
: 2554 1 1760 1766
: 就直接跳出并把result设为Y了
: 但是因为2555在地第三资料里面
: 2554 3 2553 2559
: 所以正确的result应该是N
: 请问要如何让他比对完全部的资料在决定result为多少呢?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 118.170.161.91
1F:推 pokl123:感谢 10/15 14:58