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

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

或
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
) t
where userId = 1