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

视图的查询条件有关问题

2012-03-11 
视图的查询条件问题先举例如下:1.用如下的sql查询数据,有4条记录(我想要的结果)select * from tableAunion

视图的查询条件问题
先举例如下:
1.用如下的sql查询数据,有4条记录(我想要的结果)
select * from tableA
union 
select * from tableB where colId=3 or parentcolId=3
2.用如下的sql创建视图view2
select * from tableA
union 
select * from tableB

执行select * from view2 where colId=3 or parentcolId=3,查询出来的记录只有2条了(不想要的结果)

问题:
为什么条件直接放在sql里与在程序中传的条件查询出来的结果会不一样呢?要怎样做才能达到我想要的结果呢?
请各位前辈指教一二!在此先谢过了~

[解决办法]
在UNION中,最后WHERE是对整个结果集进行条件判断

select * from tableA
union
select * from tableB where colId=3 or parentcolId=3
等同于

select * from (
select * from tableA
union
select * from tableB ) a where colId=3 or parentcolId=3

热点排行
Bad Request.