SQL合并两个语句(CROSS JOIN)

时间:2018-02-24 13:32:08

标签: mysql sql

首先,我不确定我是否正确设置了这个问题的标题。

我想做的SQL问题有点麻烦。 我现在有两个SQL语句,它们给我这样的结果:

首先

SELECT column as service from table where something = "value 1"

服务
1
2
3个

SELECT column as city from table where something = "value 2"

第二
城市 4
5个

我只想要一个SQL语句,得到如下结果:

服务城市 1 4
1 5
2 4
2 5
3 4
3 5

我很确定有一些解决方案可以实现,我只是无法找到应该如何解决。 如果有人能帮助我,我会非常感激。

2 个答案:

答案 0 :(得分:1)

您需要cross join

SELECT s.service, c.city
FROM (SELECT column as service from table where something = 'value 1'
     ) s CROSS JOIN
     (SELECT column as city from table where something = 'value 2'
     ) c

答案 1 :(得分:0)

您可以使用交叉连接

SELECT t1.column as service , t2.column as city
from table t1 
cross join table t2
where t1.something = "value 1" and   t2.something = "value 2"