1

我想通过不同的表通过单个 sql 语句检索一些信息。我想做类似的事情:

select col1,
       col2,
       (select col1 
          from table 2),
       (select col1 
          from table 3), 
  from table1 
  join table2 
  join table3

有人可以帮忙吗?

4

2 回答 2

2

首先,决定如何获取数据。如果你想使用子查询,很好,否则使用连接。例如,对于子查询,它可能如下所示:

select t1.col1,
    t1.col2,
    (select col1 from table2 t2 where t2.field = t1.field),
    (select col1 from table3 t3 where t3.field = t1.field)
from table1 t1

相反,如果你想使用连接,它可能看起来像这样:

select t1.col1,
    t1.col2,
    t2.col1,
    t3.col1
from table1 t1
    join table2 t2 on t2.field = t1.field
    join table3 t3 on t3.field = t1.field
于 2013-08-01T13:38:57.100 回答
2
select table1.col1 as t1c1, table1.col2 as t1c2, table2.col1 as t2c1, table3.col1 as t3c1
from table1 
join table2 
join table3

请注意,您将需要实际连接 table2 和 table3... 像这样的连接语句将不起作用,它们没有 ON 部分。

于 2013-08-01T13:38:01.790 回答