我在一个名为 tbl_country 和 tbl_seaport 的 SQL 数据库中有两个表
我正在尝试创建一个查询,该查询使用字段 CountryCode.
上的联接返回两个表的所有可能组合。
tbl_country
Fields: CountryID, Country, CountryCode
tbl_seaport
Fields: PortID, PortName, RoutingCode, CountryCode
我从下面开始,但我只能让它返回 250 行,这是实际的表行数。我认为它会返回 62500 (250 x 250) 行数据。
SELECT s.Country, m.Country
FROM tbl_country AS s
LEFT JOIN tbl_country AS m
ON s.CountryID = m.CountryID
关于如何实现这一点有什么想法吗?
最佳答案
试试这个
SELECT s.Country, m.Country FROM tbl_country AS s cross JOIN tbl_country AS m
编辑: 关于你的评论,你可以使用下面的查询来加入第三个表。确保删除任何重复的列名。
with cte
as
(
SELECT s.Country as sCountry , m.Country as mCountry FROM tbl_country AS s cross JOIN tbl_country AS m
)
select * from cte cross join tbl_seaport
但是,请重新考虑您的设计
关于SQL 查询设计未返回预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32091890/