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', ...
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.
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?
I’ve never seen it but it seems to make sense.
At first it orders by
s.type != 'Nails'. This isfalsefor every row that containsNailsin thetypecolumn. After that it is sorted byBolts. Again for all columns that do containBoltsas atypethis evaluates to false. And so on.A small test reveals that
falseis ordered beforetrue. So you have the following: First you get all rows withNailson top because the accordingORDER BYevaluated tofalseandfalsecomes first. The remaining rows are sorted by the secondORDER BYcriterion. And so on.