考慮以下幾點:查詢問題:是否有更好的方法可以從一個表中選擇所有記錄,並從另一個表中選擇不匹配的記錄?
**Customers**
CustomerId (PK)
LastName
FirstName
Address1
City
State
Zip
**CustomerMailingAddresses**
CustomerId (PK)/(FK)
Address1
City
State
Zip
基本上,有兩個表之間有一個一對一的關係。但是,並非Customer中的每個客戶記錄在CustomerMailingAddresses表中都有一個條目。我正在嘗試使用T-SQL(Sql Server 2008)來生成客戶名稱和addreses列表。但是,我只想從CustomerMailingAddresses返回地址,以及Customer中沒有相應條目的CustomerMailingAddresses中每個CustomerId的所有地址。換句話說,CustomerMailingAddresses中的條目(如果有的話)將作爲客戶地址的覆蓋。
我碰到了一面牆,因爲我試過的所有查詢都不起作用。我願意接受任何和所有建議。
什麼疑問你嘗試過,但沒有工作? – tkrajcar
選擇* 從客戶左參加CustomerMailingAddresses 在Customers.CustomerId = CustomerMailingAddresses.CustomerId 其中CustomerMailingAddresses.Address IS NULL - 上面有一個拼寫錯誤。 – MStp
我最初開始時使用一個左連接和CustomerMailingAddresses表作爲左表。這裏是我得到的最遠:SELECT地址,城市狀態,Zip從CustomerMailingAddresses cma LEFT JOIN客戶c ON cma.Customerid = c.CustomerId。 – TelJanini