select语句返回结果的顺序问题 .
今天看到论坛上一个朋友的回帖内容,突然意识到自己好像从来没对SELECT语句做过任何思考,即便SELECT是平时使用最多的语句。自己建了两个测试表,内容如下:
- SQL> conn scott/tiger
- Connected.
- SQL> create table a(aid int, name char(1));
- Table created.
- SQL>
- SQL> create table b(bid int, name char(1));
- Table created.
- SQL> insert into a values(1,'a');
- 1 row created.
- SQL> insert into a values(2,'b');
- 1 row created.
- SQL> insert into a values(3,'c');
- 1 row created.
- SQL> insert into b values(1,'b');
- 1 row created.
- SQL> insert into b values(2,'c');
- 1 row created.
- SQL> insert into b values(3,'a');
- 1 row created.
- SQL> commit;
- Commit complete.
- SQL> select * from a;
- AID N
- ---------- -
- 1 a
- 2 b
- 3 c
- SQL> select * from b;
- BID N
- ---------- -
- 1 b
- 2 c
- 3 a
SQL> conn scott/tiger
Connected.
SQL> create table a(aid int, name char(1));
Table created.
SQL>
SQL> create table b(bid int, name char(1));
Table created.
SQL> insert into a values(1,'a');
1 row created.
SQL> insert into a values(2,'b');
1 row created.
SQL> insert into a values(3,'c');
1 row created.
SQL> insert into b values(1,'b');
1 row created.
SQL> insert into b values(2,'c');
1 row created.
SQL> insert into b values(3,'a');
1 row created.
SQL> commit;
Commit complete.
SQL> select * from a;
AID N
---------- -
1 a
2 b
3 c
SQL> select * from b;
BID N
---------- -
1 b
2 c
3 a
1、先进行两个表的简单连接查询
- SQL> select a.aid from a,b where a.aid=b.bid;
- AID
- ----------
- 1
- 2
- 3
- SQL> select a.aid from a,b where a.name=b.name;
- AID
- ----------
- 2
- 3
- 1
SQL> select a.aid from a,b where a.aid=b.bid;
AID
----------
1
2
3
SQL> select a.aid from a,b where a.name=b.name;
AID
----------
2
3
1
起初我看到这两个表中的内容差不多,a.aid和b.bid里都是1、2、3,a.name和b.name里都是a、b、c,我就以为上面这两条查询语句将会返回一样的结果,可是实践证明我的想法是错误的,返回的结果顺序是不一样的。
别着急,看看下面这个语句的返回结果,可能你会想到些什么
- SQL> select a.aid from a,b where b.name=a.name;
- AID
- ----------
- 2
- 3
- 1
- SQL> select a.aid from b,a where a.name=b.name;
- AID
- ----------
- 1
- 2
- 3
- SQL> select a.aid from b,a where a.aid=b.bid;
- AID
- ----------
- 1
- 2
- 3
SQL> select a.aid from a,b where b.name=a.name;
AID
----------
2
3
1
SQL> select a.aid from b,a where a.name=b.name;
AID
----------
1
2
3
SQL> select a.aid from b,a where a.aid=b.bid;
AID
----------
1
2
3
当选用两张表中各自的name字段作为查询条件的时候,如果FROM 子句后面表出现的顺序不同,结果也会有所不同。
当a表出现在后面的时候,那么SELECT 语句选择a中的一条记录,然后去b表中进行扫面,看有没有符合条件的记录,当然a、b、c三个值在b表中都是有的,所以aid的结果依次为1、2、3。
可是当b表出现在后面的时候,那么SELECT语句先选择b表中的一条记录(1,'b'),到a表中进行扫面,看是否有符合条件的记录,这个时候a表中name字段为'b'的记录的响应aid为2,所以2查询结果中2就排在第一位咯。
2、看一看在SELECT中加上ORDER BY子句的情况
- SQL> select a.aid from a,b where a.name=b.name order by b.bid;
- AID
- ----------
- 2
- 3
- 1
- SQL> select a.aid from a,b where a.name=b.name order by b.name;
- AID
- ----------
- 1
- 2
- 3
- SQL> select a.aid from a,b where a.aid=b.bid order by b.name;
- AID
- ----------
- 3
- 1
- 2
- SQL> select a.aid from a,b where a.aid=b.bid order by b.bid;
- AID
- ----------
- 1
- 2
- 3
SQL> select a.aid from a,b where a.name=b.name order by b.bid;
AID
----------
2
3
1
SQL> select a.aid from a,b where a.name=b.name order by b.name;
AID
----------
1
2
3
SQL> select a.aid from a,b where a.aid=b.bid order by b.name;
AID
----------
3
1
2
SQL> select a.aid from a,b where a.aid=b.bid order by b.bid;
AID
----------
1
2
3
经过上面的解释,这个地方就不是很难理解了。
第一个SELECT语句中的ORDER BY B.BID对语句查询结果没有产生什么影响。
对于第二个SELECT语句,在排序前的结果为:
A.AID B.BID B.NAME
2 1 b
3 2 c
1 3 a
经过ORDER BY B.NAME了,就变为如下结果了:
A.AID B.BID B.NAME
1 3 a
2 1 b
3 2 c
这个时候从这个结果集中选出A.AID,当然就是1、2、3了。
下面两个以A.AID和B.BID的例子比较简单,我就不多说了,基本情况和前面一样,有兴趣的话你也可以对照着想一想这个过程,呵呵。
好了,大概就这些吧,小记一下,以后碰到SELECT相关的问题再加好了。
select语句返回结果的顺序问题 .的更多相关文章
- {MySQL的逻辑查询语句的执行顺序}一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析
MySQL的逻辑查询语句的执行顺序 阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SEL ...
- SQL Select语句完整的执行顺序(转)
SQL Select语句完整的执行顺序: 1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函 ...
- 【SQL】SQL 中Select语句完整的执行顺序
SQL Select语句完整的执行顺序: 1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函 ...
- 170607、SQL Select语句完整的执行顺序
SQL Select语句完整的执行顺序: 1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函 ...
- sql中select语句的逻辑执行顺序
下面是SELECT语句的逻辑执行顺序: FROMONJOINWHEREGROUP BYWITH CUBE or WITH ROLLUPHAVINGSELECTDISTINCTORDER BYTOP M ...
- Oracle中Select语句完整的执行顺序
oracle Select语句完整的执行顺序: .from 子句组装来自不同数据源的数据: .where 子句基于指定的条件对记录行进行筛选: .group by子句将数据划分为多个分组: .使用聚集 ...
- Select语句完整的执行顺序
Select语句完整的执行顺序:1.from子句组装来自不同数据源的数据:2.where子句基于指定的条件对记录行进行筛选:3.group by子句将数据划分为多个分组:4.使用聚集函数进行计算:5. ...
- SQL Select语句完整的执行顺序
1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函数进行计算: 5. 使用having子句筛 ...
- Oracle SQL开发 之 Select语句完整的执行顺序
查询语句语法: Select 属性 From 表 Where 条件 Group by 分组条件 Having 分组选择条件 Order by 排序条件 1.from子句组装来自不同数据源的数据: 2. ...
随机推荐
- JQuery阻止表单提交的方法总结 - 使用onsubmit()验证表单并阻止非法提交
方法1:<form onsubmit="javascript:confirm()"> 方法内返回false阻止表单提交 示例:代码检测textarea内填写的长度,未填 ...
- 自动运行native2ascii 命令的Bat文件的编写
使用eclipse开发,对于.properties文件的国际化,如果不使用插件对文件进行转码,则需要使用native2ascii命令自行对文件进行转码. 为了更方面的执行此操作,我将该 ...
- 解决ScrollView中嵌套ListView滚动效果冲突问题
在ScrollView中嵌套使用ListView,ListView只会显示一行到两行的数据.起初我以为是样式的问题,一直在对XML文件的样 式进行尝试性设置,但始终得不到想要的效果.后来在网上查了查, ...
- python之安装
1.python控制软件pyenv 依赖软件:git [root@localhost ~]# curl https://raw.github.com/yyuu/pyenv-installer/mast ...
- Python解析json字符串
{"status":0,"result":{"location":{"lng":116.47847460177,&quo ...
- Apple Catching(POJ 2385)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9978 Accepted: 4839 De ...
- 视听说加速器--AHK辅助工具
大学有视听说这门课,看起来这门课设计得非好,可是对大多数人来讲却不能按时完成.到了最后都要抓紧提速,上网找答案,辛苦的抄,有“聪明者”便找加速器来做. 我也是赶着做的人之一.抄答案太累,加速器太卡,还 ...
- linux中find指令与grep命令的组合使用
linux下find与grep管道命令的组合使用: 一.使用find与grep 1. 查找所有".h"文件(非组合命令) find /PATH -name "*.h&qu ...
- Linux下安装Perl和Perl的DBI模块
今天在虚拟机测试shell脚本的时候,有些命令使用不了. 比如说 mysqlhotcopy ,它提示Perl的版本太低. 我用的 RedHat9 的Perl才5.8.0版本...(2002年以前的) ...
- perl HTML::TreeBuilder::XPath
HTML::TreeBuilder::XPath 添加XPath 支持HTML::TreeBuilder use HTML::TreeBuilder::XPath; my $tree= HTML: ...