C# Sql从多个表中选择

C# Sql从多个表中选择,c#,sql,sql-server,tsql,C#,Sql,Sql Server,Tsql,我有三张桌子: Visitor table VisitorId (pk) Name Phone Country table CountryId (pk) Name Code Travels table TravelId (pk) CountryId (fk) VisitorId (fk) IsVisited (bool) 我希望每个国家都能创建另一个entrace in Travels表,该表将包含ISVISISTED的CountryId、VisitorId和false 更新 你应该这样

我有三张桌子:

Visitor table
VisitorId (pk)
Name
Phone


Country table
CountryId (pk)
Name
Code

Travels table
TravelId (pk)
CountryId (fk)
VisitorId (fk)
IsVisited (bool)
我希望每个国家都能创建另一个entrace in Travels表,该表将包含ISVISISTED的CountryId、VisitorId和false

更新

你应该这样写:

-- STEP2: Insert records:
INSERT INTO @Travels 
SELECT CountryId,VisitorId,0 FROM -- 0 = false for IsVisited
(
-- STEP1: first create a combination for all visitor Id and country Id 
-- and get all the combinations which are not there in existing Travels table
SELECT C.CountryId,V.VisitorId
FROM @Country C
CROSS JOIN @Visitor V
EXCEPT
SELECT CountryId,VisitorId
FROM @Travels
) AS T
-- STEP2: Insert records:
INSERT INTO @Travels 
SELECT  CountryId,VisitorId,0 FROM 
(
-- STEP1: first create a combination for all visitor Id and country Id 
SELECT C.CountryId,V.VisitorId
FROM @Country C
CROSS JOIN @Visitor V
) AS T

SELECT * FROM @Travels
否则,如果要为国家/地区和访客中的每个组合创建条目,请编写为:

-- STEP2: Insert records:
INSERT INTO @Travels 
SELECT CountryId,VisitorId,0 FROM -- 0 = false for IsVisited
(
-- STEP1: first create a combination for all visitor Id and country Id 
-- and get all the combinations which are not there in existing Travels table
SELECT C.CountryId,V.VisitorId
FROM @Country C
CROSS JOIN @Visitor V
EXCEPT
SELECT CountryId,VisitorId
FROM @Travels
) AS T
-- STEP2: Insert records:
INSERT INTO @Travels 
SELECT  CountryId,VisitorId,0 FROM 
(
-- STEP1: first create a combination for all visitor Id and country Id 
SELECT C.CountryId,V.VisitorId
FROM @Country C
CROSS JOIN @Visitor V
) AS T

SELECT * FROM @Travels

请将您目前的问题包括在内,并告诉我们您的问题所在