读书人

问一个简单的查询语句 sql查询多表合并

发布时间: 2012-02-01 16:58:19 作者: rapoo

问一个简单的查询语句 sql查询多表合并结果
select imgUrl from table1 where userId=1
select imgUrl from table2 where userId=1
select imgUrl from table3 where userId=1
select imgUrl from table4 where userId=1
select imgUrl from table5 where userId=1
select imgUrl from table6 where userId=1

这6张表查询出来有6个table,我要在sql中就把这些表的结果累加成一个表,
因为都是要查询出相同的字段imgrul
在线等

[解决办法]
select imgUrl from table1 where userId=1
union all
select imgUrl from table2 where userId=1
union all
select imgUrl from table3 where userId=1
union all
select imgUrl from table4 where userId=1
union all
select imgUrl from table5 where userId=1
union all
select imgUrl from table6 where userId=1
[解决办法]

SQL code
select imgUrl from table1 where userId=1 union allselect imgUrl from table2 where userId=1 union allselect imgUrl from table3 where userId=1 union allselect imgUrl from table4 where userId=1 union allselect imgUrl from table5 where userId=1 union allselect imgUrl from table6 where userId=1 或select * from (  select imgUrl from table1  union all  select imgUrl from table2  union all  select imgUrl from table3  union all  select imgUrl from table4  union all  select imgUrl from table5  union all  select imgUrl from table6) twhere userId = 1 

读书人网 >SQL Server

热点推荐