日期:2014-05-18  浏览次数:20624 次

有点难的SQL语句,两表查询
a表
字段如下:
id     s1     s2     s3

b表
字段如下:
id     s1     s2     d1     d2

现在想用一条SQL语句查出里面的数据

但两个表的s1,如滿足条件,那里面的都要以同一个别名显示出来,也就是要变成下表
 
id     s1

这样的格式

????

------解决方案--------------------
select id,s1 from a union all select id,s1 from b
------解决方案--------------------
楼主: 不满足怎么显示~~
------解决方案--------------------
create table taba(id varchar(10),s1 varchar(10), s2 varchar(10), s3 varchar(10))
insert into taba select '01 ', 's1 ', 's1111 ', 's1sdfg '
union all select '02 ', 's1 ', 's1111 ', 's1sdfg '
union all select '03 ', 's2 ', 's1111 ', 's1sdfg '
union all select '04 ', 's3 ', 's1111 ', 's1sdfg '
union all select '05 ', 's2 ', 's1111 ', 's1sdfg '
union all select '06 ', 's3 ', 's1111 ', 's1sdfg '
union all select '07 ', 's4 ', 's1111 ', 's1sdfg '

create table tabb(id int identity(1,1), s1 varchar(10), s2 varchar(10), d1 varchar(10), d2 varchar(10),)
insert into tabb select 's1 ', 's12g ', 'ds12425 ', 'd2edg '
union all select 's1 ', 's12g ', 'ds12425 ', 'd2edg '
union all select 's1 ', 's12g ', 'ds12425 ', 'd2edg '
union all select 's4 ', 's12g ', 'ds12425 ', 'd2edg '
union all select 's1 ', 's12g ', 'ds12425 ', 'd2edg '
union all select 's4 ', 's12g ', 'ds12425 ', 'd2edg '
union all select 's5 ', 's12g ', 'ds12425 ', 'd2edg '

select * from taba where s1 in(select distinct s1 from tabb)