我有三个表:table1,table2,table3。
表1包含:
Title1 desc LHR LONDON HEATHROW LGW LONDON GATWICK
表2包含:
Title2 desc2 W1 TESTW1 W2 TESTW2
表3包含:
vehicle desc3 SALOON SALOON DESC VIP VIP DESC EXECUTIVE EXECUTIVE DESC
我想输出所有这样的组合:
title-common title-common2 vehicle LHR W1 saloon LHR W1 VIP LHR W1 EXECUTIVE LHR W2 saloon LHR W2 VIP LHR W2 EXECUTIVE LGW W1 saloon LGW W1 VIP LGW W1 EXECUTIVE LGW W2 saloon LGW W2 VIP LGW W2 EXECUTIVE W1 LHR saloon W1 LHR VIP W1 LHR EXECUTIVE W2 LHR saloon W2 LHR VIP W2 LHR EXECUTIVE W2 LGW saloon W1 LGW VIP W1 LGW EXECUTIVE W2 LGW saloon W2 LGW VIP W2 LGW EXECUTIVE
我该如何撰写此查询?我试过交叉加入,但我没有得到我想要的。
答案 0 :(得分:2)
似乎你想要所有的对,其中一个项来自表1,另一个项来自表2,以任何顺序。您可以通过创建两个交叉连接并使用UNION来执行此操作:
(
SELECT
Title1 AS title-common,
Title2 AS title-common2,
vehicle
FROM table1
CROSS JOIN table2
CROSS JOIN table3
)
UNION
(
SELECT
Title2 AS title-common,
Title1 AS title-common2,
vehicle
FROM table1
CROSS JOIN table2
CROSS JOIN table3
)