d1g*_*r0n 3 sql t-sql sql-server
我在一个名为tbl_country和tbl_seaport的SQL数据库中有两个表
我正在尝试创建一个查询,该查询使用字段连接返回两个表的所有可能组合 CountryCode.
tbl_country
Fields: CountryID, Country, CountryCode
tbl_seaport
Fields: PortID, PortName, RoutingCode, CountryCode
Run Code Online (Sandbox Code Playgroud)
我从下面开始,但我只能得到它返回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
Run Code Online (Sandbox Code Playgroud)
关于如何实现这一点的任何想法?
试试这个
SELECT s.Country, m.Country FROM tbl_country AS s cross JOIN tbl_country AS m
Run Code Online (Sandbox Code Playgroud)
编辑: 关于您的评论,您可以使用以下查询加入第3表.确保删除任何重复列名称.
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
Run Code Online (Sandbox Code Playgroud)
但是,请重新考虑你的设计
| 归档时间: |
|
| 查看次数: |
70 次 |
| 最近记录: |