简体   繁体   English

查询问题:是否有更好的方法从一个表中选择所有记录,以及从另一表中选择不匹配的记录?

[英]Query question: Is there a better way to select all records from one table, along with unmatched records from another table?

Consider the following: 考虑以下:

**Customers**
CustomerId (PK)
LastName
FirstName
Address1
City
State
Zip

**CustomerMailingAddresses**
CustomerId (PK)/(FK)
Address1
City
State
Zip

Basically, there's a one-to-one relationship between the two tables. 基本上,两个表之间存在一对一的关系。 However, not every customer record in Customers has an entry in the CustomerMailingAddresses table. 但是,并非Customer中的每个客户记录在CustomerMailingAddresses表中都有一个条目。 I'm attempting to use T-SQL (Sql Server 2008) to generate a list of customer names and addreses. 我正在尝试使用T-SQL(Sql Server 2008)生成客户名称和地址列表。 However, I only want to return the address from CustomerMailingAddresses as well as all addresses from Customers that do not have a corresponding entry for each CustomerId in CustomerMailingAddresses. 但是,我只想从CustomerMailingAddresses返回地址,以及从Customer中返回的所有地址,这些地址在CustomerMailingAddresses中没有每个CustomerId的对应条目。 In other words, the entry in CustomerMailingAddresses (if there is one), will act as an override for the address in Customers. 换句话说,CustomerMailingAddresses中的条目(如果有的话)将充当客户中地址的替代。

I've hit a wall, as none of the queries I've tried will work. 我碰壁了,因为我尝试过的所有查询都不起作用。 I'm open to any and all suggestions. 我愿意接受任何建议。

One option is to use COALESCE 一种选择是使用COALESCE

select
    c.CustomerId,
    COALESCE(m.Address1, c.Address1) as Address1,
    COALESCE(m.City, c.City) as City,
    COALESCE(m.State, c.State) as State,
    COALESCE(m.Zip, c.Zip) as Zip
from Customers c
left join CustomerMailingAddresses m on m.CustomerId = c.CustomerId

How about 怎么样

SELECT * FROM Customers
MINUS
SELECT Customers.*
       FROM Customers, CustomerMailingAddresses
       WHERE Customers.CustomerId = CustomerMailingAddresses.CustomerId

(sorry if my SQL is a bit rusty) (对不起,如果我的SQL有点生锈)

暂无
暂无

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

相关问题 SQL查询-从一个表中选择全部,在另一个表中匹配记录 - SQL Query - select all from one table with matching records in another Select 基于条件的一个表中的所有记录,而不是另一个表中的所有记录 - Select all records from one table not in another table based on a condition 根据另一个表中的记录选择一个表中的记录 - Select records from one table based on records from another table select 如何将一张表中的所有数据与另一张表中的记录匹配,先选数据。 全部在一个查询中 - How to select all data from one table and records from another table matching data in first selection. All in one query 显示一个表中的所有记录,并显示另一个表中的匹配记录 - displaying all records from one table and matching records from another 如何选择表中的所有记录(不包括另一个记录) - How to select all records in a table excluding records from another 如何 select 一个表中的所有记录在另一个表中不存在于另一个表中的某些条件下? - How to select all records from one table that do not exist in another table for certain condition in another table? 从两个表中选择不匹配的记录,并在第二个表上使用过滤器 - Select unmatched records from two tables with a filter on second table SQL查询可从一个表中获取所有记录,并在另一个表上进行连接,包括任何其他唯一记录 - SQL Query to get all records from one table and join on another including any additional unique records 从一个表中选择所有记录,并从另一表中选择引用值? - Select all records from one table and referenced values from another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM