使用LEFT OUTER JOIN,這從兩個表的左和右表和不匹配的記錄都返回匹配的記錄副本。這裏是我試過的測試代碼:
CREATE TABLE AddressTypes(
AddressType SMALLINT NOT NULL,
[Description] NVARCHAR(50))
ALTER TABLE AddressTypes
ADD CONSTRAINT PK_AddressTypes_AddressType
PRIMARY KEY (AddressType)
CREATE TABLE [Address] (
AddressID INT NOT NULL,
AddressType SMALLINT,
Address1 NVARCHAR(50),
Address2 NVARCHAR(50),
City NVARCHAR(50),
State CHAR(2))
ALTER TABLE [Address]
ADD CONSTRAINT PK_Address_AddressID
PRIMARY KEY (AddressID)
ALTER TABLE [Address]
ADD CONSTRAINT FK_address_addresstypes_addresstype
FOREIGN KEY (AddressType) REFERENCES AddressTypes(AddressType)
INSERT INTO AddressTypes VALUES (1, 'home'), (2, 'office'),
(3, 'vacation'), (4, 'hotel')
INSERT INTO address VALUES (1, 1, 'piping', '1232', 'Austin', 'TX')
-- Here is the query that outputs the result set.
SELECT AT.AddressType AS [Type], A.Address1, A.Address2, A.City, A.State
FROM AddressTypes AT
LEFT OUTER JOIN address A
ON AT.AddressType = A.AddressType
我不明白你的問題,你能展示你想要產生這個結果的兩個表的樣本。 – automatic 2012-07-12 22:57:09
你可能想編輯你的標題,所以它不建議一個可能不正確的解決方案(交叉連接可能不是答案)。 – automatic 2012-07-12 22:58:56
下面是表http://tinypic.com/view.php?pic=28078xv&s=6 – VAAA 2012-07-12 23:06:25