[英]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.
我愿意接受任何建议。
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.