+ -
当前位置:首页 → 问答吧 → 一句sql不知道怎么写

一句sql不知道怎么写

时间:2011-11-19

来源:互联网

表1 字段 a,b,c

表2 字段 d,e,f

表2的e,f字段存的是表1的a



现在要读  

表2.e=表1.a 的表1的所有字段数据

表2.f=表1.a 的表1的所有字段数据

还有 对应的表2的字段数据

用一句sql

不知道表达清楚了没

作者: csrr2012   发布时间: 2011-11-19

SQL code
select a.*,b.*
from tb1 a
join
  (select d,e from tb2 union select d,f from tb2) b
on a.a=b.e

作者: josy   发布时间: 2011-11-19

SQL code
select *
from tb2 a join tb1 b on a.e = b.a
           join tb1 c on a.f = c.a

作者: AcHerat   发布时间: 2011-11-19

不行就换left join。

作者: AcHerat   发布时间: 2011-11-19

SQL code
select a.*,b.* from t1 a inner join t2 b on a.a=b.e or a.a=b.f

作者: qianjin036a   发布时间: 2011-11-19

SQL code
create table t1(a int,b int,c int)
insert into t1 select 1,234,534
insert into t1 select 2,324,439
insert into t1 select 3,94,9438
insert into t1 select 4,843,92
insert into t1 select 5,985,894
create table t2(d varchar(10),e int,f int)
insert into t2 select 'aaa',2,19
insert into t2 select 'bbb',5,33
insert into t2 select 'ccc',10,4
go
select a.*,b.* from t1 a inner join t2 b on a.a=b.e or a.a=b.f
/*
a           b           c           d          e           f
----------- ----------- ----------- ---------- ----------- -----------
2           324         439         aaa        2           19
5           985         894         bbb        5           33
4           843         92          ccc        10          4

(3 行受影响)

*/
go
drop table t1,t2

作者: qianjin036a   发布时间: 2011-11-19

热门下载

更多