gpt4 book ai didi

mysql - inner self join的呈现顺序

转载 作者:行者123 更新时间:2023-11-29 05:29:42 24 4
gpt4 key购买 nike

如果我有如下表格:

+------+-------+   
| sno | cno |
+------+-------+
| 1 | CS112 |
| 1 | CS113 |
| 1 | CS114 |
| 2 | CS112 |
| 3 | CS112 |
| 3 | CS114 |
| 4 | CS112 |
| 4 | CS113 |
| 5 | CS113 |
| 6 | CS113 |
| 6 | CS114 |
+------+-------+

如果我在 sno 上进行内部自连接,我希望看到的行按以下顺序排列:

+------+-------+------+-------+  
| sno | cno | sno | cno |
+------+-------+------+-------+
| 1 | CS112 | 1 | CS112 |
| 1 | CS112 | 1 | CS113 |
| 1 | CS112 | 1 | CS114 |
| 1 | CS113 | 1 | CS112 |
| 1 | CS113 | 1 | CS113 |
| 1 | CS113 | 1 | CS114 |
etc
+------+-------+------+-------+

但是顺序是

+------+-------+------+-------+  
| sno | cno | sno | cno |
+------+-------+------+-------+
| 1 | CS112 | 1 | CS112 |
| 1 | CS113 | 1 | CS112 |
| 1 | CS114 | 1 | CS112 |
| 1 | CS112 | 1 | CS113 |
| 1 | CS113 | 1 | CS113 |
| 1 | CS114 | 1 | CS113 |
| 1 | CS112 | 1 | CS114 |
etc
+------+-------+------+-------+

即我希望看到左侧的每一行都对骑行中的每一行重复。相反的情况发生了。 IE。它似乎需要所有行并将其与连接右侧的第一行匹配。
为什么顺序是这样的?

最佳答案

除非您使用 order by 子句(或在 MySQL 中,group by 子句),否则您不应该期望查询结果的顺序。

不幸的是,我没有在 MySQL 文档中找到明确说明这一点的引用资料。最接近的是这个报价 here :

You may have noticed in the preceding examples that the result rows are displayed in no particular order. It is often easier to examine query output when the rows are sorted in some meaningful way. To sort a result, use an ORDER BY clause.

但是,ANSI 标准确实指定查询结果(和表)是无序的,除非指定了 order by 子句。

关于mysql - inner self join的呈现顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16092180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com