简体   繁体   English

如果表2也有数据,如何仅从表1中提取数据

[英]How to only pull data from table 1 if table 2 also have data

My problem is, I have two tables that we are using for shipping and this is what is happening. 我的问题是,我有两个用于运输的表,而这就是正在发生的情况。 If ca.addr_1 has data is fine but if there is data in c.addr_2 then show it. 如果ca.addr_1有数据可以,但是c.addr_2中有数据,则显示它。 And what I want is if ca.addr has the data on some line use it but if all lines are null then use c.addr. 我想要的是如果ca.addr在某行上有数据,请使用它,但如果所有行均为空,则使用c.addr。

CO.SHIP_TO_ADDR_NO AS Addr_No,
ISNULL(CA.NAME, C.NAME) AS Name,
ISNULL(CA.ADDR_1, C.ADDR_1) AS Addr_1,
ISNULL(CA.ADDR_2, C.ADDR_2) AS Addr_2,
ISNULL(CA.ADDR_3, C.ADDR_3) AS Addr_3,
ISNULL(CA.CITY, C.CITY) AS City, 
ISNULL(CA.STATE, C.STATE) AS State,
ISNULL(CA.ZIPCODE, C.ZIPCODE) AS ZipCode,
ISNULL(CA.COUNTRY, C.COUNTRY) AS Country, 

CASE WHEN should allow you to do what you need. CASE WHEN应该允许您做您需要的事情。

CASE WHEN ca.addr1 IS NULL AND CA.ADDR_2 IS NULL AND CA.ADDR_3 IS NULL
THEN c.addr
ELSE WHEN ....

You can add in as many variations using ELSE WHEN as you need to account for whatever permutation of address results you want to select. 您可以使用ELSE WHEN来添加尽可能多的变体,以考虑要选择的地址结果的任何排列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM