今天看到论坛上一个朋友的回帖内容,突然意识到自己好像从来没对SELECT语句做过任何思考,即便SELECT是平时使用最多的语句。自己建了两个测试表,内容如下:

  1. SQL> conn scott/tiger
  2. Connected.
  3. SQL> create table a(aid int, name char(1));
  4. Table created.
  5. SQL>
  6. SQL> create table b(bid int, name char(1));
  7. Table created.
  8. SQL> insert into a values(1,'a');
  9. 1 row created.
  10. SQL> insert into a values(2,'b');
  11. 1 row created.
  12. SQL> insert into a values(3,'c');
  13. 1 row created.
  14. SQL> insert into b values(1,'b');
  15. 1 row created.
  16. SQL> insert into b values(2,'c');
  17. 1 row created.
  18. SQL> insert into b values(3,'a');
  19. 1 row created.
  20. SQL> commit;
  21. Commit complete.
  22. SQL> select * from a;
  23. AID N
  24. ---------- -
  25. 1 a
  26. 2 b
  27. 3 c
  28. SQL> select * from b;
  29. BID N
  30. ---------- -
  31. 1 b
  32. 2 c
  33. 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、先进行两个表的简单连接查询

  1. SQL> select a.aid from a,b where a.aid=b.bid;
  2. AID
  3. ----------
  4. 1
  5. 2
  6. 3
  7. SQL> select a.aid from a,b where a.name=b.name;
  8. AID
  9. ----------
  10. 2
  11. 3
  12. 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,我就以为上面这两条查询语句将会返回一样的结果,可是实践证明我的想法是错误的,返回的结果顺序是不一样的。

别着急,看看下面这个语句的返回结果,可能你会想到些什么

  1. SQL> select a.aid from a,b where b.name=a.name;
  2. AID
  3. ----------
  4. 2
  5. 3
  6. 1
  7. SQL> select a.aid from b,a where a.name=b.name;
  8. AID
  9. ----------
  10. 1
  11. 2
  12. 3
  13. SQL> select a.aid from b,a where a.aid=b.bid;
  14. AID
  15. ----------
  16. 1
  17. 2
  18. 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子句的情况

  1. SQL> select a.aid from a,b where a.name=b.name order by b.bid;
  2. AID
  3. ----------
  4. 2
  5. 3
  6. 1
  7. SQL> select a.aid from a,b where a.name=b.name order by b.name;
  8. AID
  9. ----------
  10. 1
  11. 2
  12. 3
  13. SQL> select a.aid from a,b where a.aid=b.bid order by b.name;
  14. AID
  15. ----------
  16. 3
  17. 1
  18. 2
  19. SQL> select a.aid from a,b where a.aid=b.bid order by b.bid;
  20. AID
  21. ----------
  22. 1
  23. 2
  24. 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相关的问题再加好了。

ORACLE表连接方式可以参考这篇文章

select语句返回结果的顺序问题 .的更多相关文章

  1. {MySQL的逻辑查询语句的执行顺序}一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析

    MySQL的逻辑查询语句的执行顺序 阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SEL ...

  2. SQL Select语句完整的执行顺序(转)

    SQL Select语句完整的执行顺序: 1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函 ...

  3. 【SQL】SQL 中Select语句完整的执行顺序

    SQL Select语句完整的执行顺序: 1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函 ...

  4. 170607、SQL Select语句完整的执行顺序

    SQL Select语句完整的执行顺序: 1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函 ...

  5. sql中select语句的逻辑执行顺序

    下面是SELECT语句的逻辑执行顺序: FROMONJOINWHEREGROUP BYWITH CUBE or WITH ROLLUPHAVINGSELECTDISTINCTORDER BYTOP M ...

  6. Oracle中Select语句完整的执行顺序

    oracle Select语句完整的执行顺序: .from 子句组装来自不同数据源的数据: .where 子句基于指定的条件对记录行进行筛选: .group by子句将数据划分为多个分组: .使用聚集 ...

  7. Select语句完整的执行顺序

    Select语句完整的执行顺序:1.from子句组装来自不同数据源的数据:2.where子句基于指定的条件对记录行进行筛选:3.group by子句将数据划分为多个分组:4.使用聚集函数进行计算:5. ...

  8. SQL Select语句完整的执行顺序

    1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函数进行计算: 5. 使用having子句筛 ...

  9. Oracle SQL开发 之 Select语句完整的执行顺序

    查询语句语法: Select 属性 From 表 Where 条件 Group by 分组条件 Having 分组选择条件 Order by 排序条件 1.from子句组装来自不同数据源的数据: 2. ...

随机推荐

  1. shell中的环境变量

    局部(local)环境变量 定义局部环境变量的方式如下: variableName=value 需要注意的是variableName前面没有$符号,并且=两边没有空格. 局部环境变量只能在当前shel ...

  2. __m128i的理解[转]

    __m128i被称为128bits的整数,当我们对其赋值时,调用 __m128i _mm_set1_epi32(int i) Sets the four signed 32-bit integer v ...

  3. 负载均衡集群之LVS配置命令

    ipvs/ipvsadm 添加集群服务--> ipvsadm -A|E -t|u|f VIP[:Port] -s scheduler [-p timeout] [-O] [-M netmask] ...

  4. python----特性002

    python特性002:特性是可继承的 #!/usr/local/python3.5/bin/python3 class Person(object): def __init__(self,name) ...

  5. Blog透视镜

    Blog透视镜,提供了Blog代码示例,文章和教程,可以帮助你建置博客. 网站名称:Blog透视镜 网站地址:http://blog.openyu.org

  6. 基于epoll的聊天室程序

    epoll相对于poll和select这两个多路复用的I/O模型更加的高效.epoll的函数很简单,麻烦的地方在于水平触发和边沿触发. 用张图来说明下 ET(边沿)只是在状态反转时触发,比如从不可读到 ...

  7. JavaScript 之 Cookie

    JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的. 而cookie是运行在客户端的,所以可以用JS来设置cookie. 假设有这样一 ...

  8. hdu 4620 Fruit Ninja Extreme

    Fruit Ninja Extreme Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. Maximum Depth of Binary Tree 解答

    Question Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...

  10. hdu1172猜数字(暴力枚举)

    猜数字 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...