如下图,要从同一个
表输入主或副卡卡号中找出主卡卡号……
但是同一个表查了2次好像效率不好,请问有哪种方法可能写得更好?
select cardNum from localcardnum as a
join
localcardnum as b
on a.customerID=b.customerID
where a.ParentCard=0 and b.CardNum='xxxxxx'
假设输入的主卡或副卡号为:XXXXX
自连接试一下:
select A.cardNum from localcardnum A ,localcardnum B
where (A.cardNum='XXXXX' and ParentCard=0)
or ( B.cardNum='XXXXX' and A.CustomerId = B.ParentCard )
用sql server的执行计划分析一下你的sql语句,看看主要的花销是在哪个方面
where cardnum='XXX' or parentCard='XXX'
select (case ParentCard when 0 then CustomerId else ParentCard end) as CustomerId from localcardnum where cardnum ='XXX'