首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

问一个简单的查询语句 sql查询多表合并结果,该怎么解决

2012-02-01 
问一个简单的查询语句 sql查询多表合并结果select imgUrl from table1 where userId1select imgUrl from

问一个简单的查询语句 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 

热点排行