自定义ORDER BY说明 [英] Custom ORDER BY Explanation

查看:126
本文介绍了自定义ORDER BY说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我前一段时间发现了这个,从那以后一直在使用它;但是,今天看,我意识到我不完全理解它为什么起作用。有人可以帮我了解一下吗?

I found this some time ago and have been using it since; however, looking at it today, I realized that I do not fully understand why it works. Can someone shed some light on it for me?

ORDER BY  s.type!= 'Nails',
          s.type!= 'Bolts',
          s.type!= 'Washers',
          s.type!= 'Screws',
          s.type!= 'Staples',
          s.type!= 'Nuts', ...

如果我按s.type订购,它按字母顺序排序。如果我使用上面的示例,则它使用与行位置相同的顺序。我不了解使用!=。如果我使用=,它将以相反的顺序出现。

If I order by s.type, it orders alphabetically. If I use the example above it uses the same order as the line positions. What I don't understand is the use of !=. If I use = it appears in the opposite order. I cannot wrap my head around the concept of this.

这会导致我的理由是,使用=代替上面的!=将把Nails放在第一位,但不是,它放在最后。我猜我的问题是:在这种情况下,为什么我必须使用!=,而不是=?

It would reason to me that using = in place of the !='s above would place Nails first in position, but it does not, it place it in the last. I guess my question is this: Why do i have to use !=, not = in this situation?

推荐答案

起初它是按 s.type!='Nails'。对于包含类型 Nails 的每一行,这是 false >列。之后,按螺栓进行排序。同样,对于所有包含螺栓作为类型的列,其结果均为false。等等。

At first it orders by s.type != 'Nails'. This is false for every row that contains Nails in the type column. After that it is sorted by Bolts. Again for all columns that do contain Bolts as a type this evaluates to false. And so on.

一个小测试表明, false true之前被订购了。因此,您具有以下条件:首先,您获得的所有行都以 Nails 开头,因为根据 ORDER BY 评估为 false false 首先出现。其余的行按第二个 ORDER BY 准则排序。

A small test reveals that false is ordered before true. So you have the following: First you get all rows with Nails on top because the according ORDER BY evaluated to false and false comes first. The remaining rows are sorted by the second ORDER BY criterion. And so on.


 type     | != Nails | != Bolts | != Washers
'Nails'   | false    | true     | true
'Bolts'   | true     | false    | true
'Washers' | true     | true     | false

这篇关于自定义ORDER BY说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆