Home >Database >Mysql Tutorial >Oracle中select 1和select *的区别

Oracle中select 1和select *的区别

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 16:56:503714browse

创建myt表并插入数据,如下:create table myt(name varchar2,create_time date) insert into myt values(

创建myt表并插入数据,如下:

create table myt(name varchar2,create_time date)

 insert into myt values('john',to_date(sysdate,'DD-MON-YY'));

 insert into myt values('tom',to_date(sysdate,'DD-MON-YY'));

 insert into myt values('lili',to_date(sysdate,'DD-MON-YY')); 

 在sql*plus中显示如下:

SQL> select * from myt;
 
NAME       CREATE_TIME
---------- -----------
john       2010-5-19
tom        2010-5-19
lili       2010-5-19
 
SQL> select 1 from myt;
 
         1
----------
         1
         1
         1

SQL> select 0 from myt;
 
         0
----------
         0
         0
         0
从以上结果 可以看到,select constant fromtable 对所有行返回对应的常量值(具体应用见下面),

而select * from table则返回所有行对应的所有列。

select 1常用在exists子句中,检测符合条件记录是否存在。

如select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ;
T1数据量小而T2数据量非常大时,T1“select 1”这里的 “1”其实是无关紧要的,换成“*”也没问题,它只在乎括号里的数据能不能查找出来,是否存在这样的记录,如果存在where 条件成立。
如下示例:

SQL> select 1/0 from dual;
 
select 1/0 from dual
 
ORA-01476: 除数为 0

SQL> select * from myt where  exists(select 1/0 from dual);
 
NAME       CREATE_TIME
---------- -----------
john       2010-5-19
tom        2010-5-19
lili       2010-5-19

没有返回错误,说明,,并没有让select语句参与计算。

在exists子句中写select *并不返回表中所有的列,只是检测符合条件记录是否存在。

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn