内连接中的排序依据
我将内部连接放入查询中。我得到了结果,但不知道数据如何输入输出。任何人都可以告诉我内部连接如何匹配数据。下面我显示了一个图像。有两张桌子(一张或两张桌子)。
根据我的说法,第一行应该是 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).
According to me that first row it should be Mohit but output is different. Please tell me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在
SQL
中,除非您在ORDER BY
子句中指定,否则不会定义输出顺序。试试这个:
In
SQL
, the order of the output is not defined unless you specify it in theORDER BY
clause.Try this:
避免在主查询中使用
SELECT *
。避免重复列:
JOIN
条件确保One.One_Name
和two.One_Name
相等,因此您不需要在SELECT
子句。避免重复的列名:使用“别名”重命名
One.ID
和Two.ID
。使用
SELECT
子句中的列名称(适用时为“alises”)添加一个ORDER BY
子句。建议重写:
Avoid
SELECT *
in your main query.Avoid duplicate columns: the
JOIN
condition ensuresOne.One_Name
andtwo.One_Name
will be equal therefore you don't need to return both in theSELECT
clause.Avoid duplicate column names: rename
One.ID
andTwo.ID
using 'aliases'.Add an
ORDER BY
clause using the column names ('alises' where applicable) from theSELECT
clause.Suggested re-write:
如果您希望数据以某种方式返回,则必须对其进行排序。当您说您希望“
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 canorder by
any other column.在第一个查询末尾添加
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.
默认情况下,SQL 不返回任何排序,因为这样速度更快。它不必先检查您的数据,然后决定要做什么。
您需要添加一个 order by 子句,并且可能按照您期望的 ID 进行排序。 (名称重复,因此我假设您想要 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)
我发现这是加入时的一个问题,但您可能会发现此博客对于了解加入在后面的工作原理很有用。
连接如何工作..
[已编辑]
@Shree 谢谢你指出这一点。
关于Merge 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...
.