今天一个同事发现了一个特别诡异的问题,查看一个表的时候 ='小灵通' 没有问题,但是多个表union起来以后 就查不出任何数据了。
select * from temp1 where prod_name='小灵通' 可以查出来500多行记录。
用 select * from temp1
union all
select * from temp2
union all
select * from temp3
where prod_name='小灵通'
则查不出任何记录,于是就感觉到特别的奇怪,仔细查看了语句,没有任何问题,于是查看这几个表的生成过程,注意到有类似如下的语句:
create table temp1 as select '小灵通' prod_name from dual;
create table temp2 as select 'E6E8宽带' prod_name from dual;
然后查看temp1和temp2的表结构,发现prod_name字段有所不同,一个是char(6),还有一个是char(8),然后还发现在union all 之后temp1的 prod_name里面出现了 空格,这就是造成union all之后不能选择出记录的原因。确实很奇怪,后面用了一个函数 TRIM() 删除左右两边出现的空格,用了之后就没有问题了。
记录一下 以供以后注意避免这种错误。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12216142/viewspace-594766/,如需转载,请注明出处,否则将追究法律责任。
广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员