视图的查询条件问题
先举例如下:
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