案件条件然后使用两个表

时间:2012-07-23 19:28:10

标签: sql sql-server sql-server-2008 tsql

select firstname,lastname 
from table1 (nolock) 
left outer join table2 
    on table1.ID=table2.ID
where table1.billingaddress like 'Lake street''

尝试使用像table1.billingaddress isnull时我需要用

替换它

table2.shippingaddress

select firstname,lastname 
from table1 (nolock) 
left outer join table2 
    on table1.ID=table2.ID
where case billingaddress isnull then shippingaddress like 'Lake street'

我没有得到理想的结果,请分享您的想法如何使用SQL查询实现这一点。

3 个答案:

答案 0 :(得分:2)

select firstname,lastname 
from table1 (nolock) 
left outer join table2 on table1.ID=table2.ID
where isnull(billingaddress, shippingaddress) like 'Lake street'

答案 1 :(得分:1)

来自评论:当帐单邮寄地址为空时,会检查送货地址是否与湖街一样,并且将返回所有带有非空帐单邮寄地址的行,如果送货地址为'Lake'则会返回街道“

SELECT firstname,lastname 
FROM table1 (nolock) 
LEFT OUTER JOIN table2 on table1.ID=table2.ID
WHERE billingaddress IS NOT NULL OR (billingaddress IS NULL AND shippingaddress LIKE 'Lake street')

仅基于问题的第一次尝试:

此查询将获取帐单邮寄地址就像“湖街”的任何内容,但如果帐单邮寄地址为空,则会收到送货地址如“湖街”的任何内容。

SELECT firstname,lastname 
FROM table1 (nolock) 
LEFT OUTER JOIN table2 on table1.ID=table2.ID
WHERE COALESCE(billingaddress, shippingaddress) LIKE 'Lake street'

编辑:就像'Lake street'有点毫无意义,请告诉我你至少在那里使用通配符?

答案 2 :(得分:0)

所以你想要的是,我可以说清楚,是:

如果Table1.billingaddress不为null,则为“LIKE billingaddress”

其他“LIKE shippingaddress”?

如果我理解正确的话,我会把它编辑成答案。

select firstname,lastname 
from table1 (nolock) 
left outer join table2 on table1.ID=table2.ID
where CASE WHEN billingAddress IS NULL THEN shippingAddress ELSE billingAddress END LIKE     'Whatever'