内连接中的排序依据

发布于 2024-09-30 11:07:52 字数 191 浏览 7 评论 0原文

我将内部连接放入查询中。我得到了结果,但不知道数据如何输入输出。任何人都可以告诉我内部连接如何匹配数据。下面我显示了一个图像。有两张桌子(一张或两张桌子)。

alt text

根据我的说法,第一行应该是 Mohit,但输出不同。请告诉我。

I am putting inner join in my query.I have got the result but didn't know that how the data is coming in output.Can anyone tell me that how the Inner join matching the data.Below I am showing a image.There are two table(One or Two Table).

alt text

According to me that first row it should be Mohit but output is different. Please tell me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

So要识趣 2024-10-07 11:07:52

SQL 中,除非您在ORDER BY 子句中指定,否则不会定义输出顺序。

试试这个:

SELECT  *
FROM    one
JOIN    two
ON      one.one_name = two.one_name
ORDER BY
        one.id

In SQL, the order of the output is not defined unless you specify it in the ORDER BY clause.

Try this:

SELECT  *
FROM    one
JOIN    two
ON      one.one_name = two.one_name
ORDER BY
        one.id
终遇你 2024-10-07 11:07:52

避免在主查询中使用 SELECT *

避免重复列:JOIN 条件确保 One.One_Nametwo.One_Name 相等,因此您不需要在SELECT 子句。

避免重复的列名:使用“别名”重命名 One.IDTwo.ID

使用 SELECT 子句中的列名称(适用时为“alises”)添加一个 ORDER BY 子句。

建议重写:

SELECT T1.ID AS One_ID, T1.One_Name, 
       T2.ID AS Two_ID, T2.Two_name
  FROM One AS T1
       INNER JOIN two AS T2
          ON T1.One_Name = T2.One_Name
 ORDER 
    BY One_ID;

Avoid SELECT * in your main query.

Avoid duplicate columns: the JOIN condition ensures One.One_Name and two.One_Name will be equal therefore you don't need to return both in the SELECT clause.

Avoid duplicate column names: rename One.ID and Two.ID using 'aliases'.

Add an ORDER BY clause using the column names ('alises' where applicable) from the SELECT clause.

Suggested re-write:

SELECT T1.ID AS One_ID, T1.One_Name, 
       T2.ID AS Two_ID, T2.Two_name
  FROM One AS T1
       INNER JOIN two AS T2
          ON T1.One_Name = T2.One_Name
 ORDER 
    BY One_ID;
以酷 2024-10-07 11:07:52

如果您希望数据以某种方式返回,则必须对其进行排序。当您说您希望“Mohit”成为第一行时,我假设您这么说是因为“Mohit”是 [One ] 表。然而,当SQL Server连接表时,它不一定按照你想象的顺序连接。

如果您希望返回 [One] 中的第一行,请尝试按 [One].[ID] 排序。或者,您可以任何其他列排序。

You have to sort it if you want the data to come back a certain way. When you say you are expecting "Mohit" to be the first row, I am assuming you say that because "Mohit" is the first row in the [One] table. However, when SQL Server joins tables, it doesn't necessarily join in the order you think.

If you want the first row from [One] to be returned, then try sorting by [One].[ID]. Alternatively, you can order by any other column.

玩心态 2024-10-07 11:07:52

在第一个查询末尾添加 ORDER BY ONE.ID ASC

默认情况下没有排序。

Add an ORDER BY ONE.ID ASC at the end of your first query.

By default there is no ordering.

扶醉桌前 2024-10-07 11:07:52

默认情况下,SQL 不返回任何排序,因为这样速度更快。它不必先检查您的数据,然后决定要做什么。

您需要添加一个 order by 子句,并且可能按照您期望的 ID 进行排序。 (名称重复,因此我假设您想要 One.ID)

select * From one
inner join two
ON one.one_name = two.one_name
ORDER BY one.ID

SQL doesn't return any ordering by default because it's faster this way. It doesn't have to go through your data first and then decide what to do.

You need to add an order by clause, and probably order by which ever ID you expect. (There's a duplicate of names, thus I'd assume you want One.ID)

select * From one
inner join two
ON one.one_name = two.one_name
ORDER BY one.ID
浸婚纱 2024-10-07 11:07:52

我发现这是加入时的一个问题,但您可能会发现此博客对于了解加入在后面的工作原理很有用。
连接如何工作..

[已编辑]
@Shree 谢谢你指出这一点。
关于Merge Join 的段落。它提到了连接如何工作......

喜欢
散列连接、合并连接由两个步骤组成。首先,两个表
join 按 join 属性排序。只需两个即可完成此操作
通过外部合并排序遍历每个表。最后,
当从中提取下一个有序元素时生成结果元组
各表及连接属性进行比较

I found this to be an issue when joining but you might find this blog useful in understanding how Joins work in the back.
How Joins Work..

[Edited]
@Shree Thank you for pointing that out.
On the paragraph of Merge Join. It mentions on how joins work...

Like
hash join, merge join consists of two steps. First, both tables of the
join are sorted on the join attribute. This can be done with just two
passes through each table via an external merge sort. Finally, the
result tuples are generated as the next ordered element is pulled from
each table and the join attributes are compared

.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文