Oracle别名使用问题 表结构与表数据: create table fruit(fruitname varchar2(20),product varchar2(20),price varchar2(20)); insert into fruit values('apple','china','12'); insert into fruit values('apple','japan','22'); insert into fruit values('apple','usa','33'); insert into fruit values('orange','china','10'); insert into fruit values('banana','china','31'); insert into fruit values('peach','usa','30');
问题: 1> select fruitname f from fruit where f='apple'; //无效的标识符 2> select fruitname f from fruit where fruitname='apple'; //正确 3> select fruitname,count(*) c from fruit group by fruitname having c>0; //无效的标识符 4> select fruitname,count(*) c from fruit group by fruitname having count(*)>0; //正确 5> select fruitname,count(*) c from fruit group by fruitname having count(*)>0 order by c; //正确
上述五个问题的运行结果为什么是那样,请解释下?
在from子句中为表指定了别名,表的实际名称是不是被覆盖了?
------解决方案--------------------
SQL code
select fruitname f from fruit where f='apple';--此处在执行where条件时,列并没有参加计算呢,因此列的别名此时并不存在,会有错,这和执行顺序有关系,你可以这样写
select fruitname f from fruit where fruitname='apple' order by f;--此时别名不会有错,因order by执行在最后
其它类似,看下执行顺序你就清楚了,别名列在下面执行顺序中的第六步
------解决方案--------------------