sql-order-by

Sorting string column containing numbers in SQL?

≡放荡痞女 提交于 2019-11-26 11:09:39
问题 I am trying to sort string column ( containing numbers ). // SELECT `name` FROM `mytable` ORDER BY `name` ASC +----------+ +-- name --+ +----------+ +-- a 1 ---+ +-- a 12 --+ +-- a 2 ---+ +-- a 3 ---+ You see natural sorting algorithm of Mysql is placing a 12 after a 1 ( which is ok for most apps ), But I have unique needs, so I want result should be sorted like this. +----------+ +-- name --+ +----------+ +-- a 1 ---+ +-- a 2 ---+ +-- a 3 ---+ +-- a 12 --+ Is it possible with just SQL , or I

Sorting related items in a Django template

你离开我真会死。 提交于 2019-11-26 10:33:50
问题 Is it possible to sort a set of related items in a DJango template? That is: this code (with HTML tags omitted for clarity): {% for event in eventsCollection %} {{ event.location }} {% for attendee in event.attendee_set.all %} {{ attendee.first_name }} {{ attendee.last_name }} {% endfor %} {% endfor %} displays almost exactly want I want. The only thing I want to change is I the list of attendees to be sorted by last name. I\'ve tried saying something like this: {% for event in events %} {{

Why do NULL values come first when ordering DESC in a PostgreSQL query?

∥☆過路亽.° 提交于 2019-11-26 09:59:13
问题 When would you ever want NULLS first when ordering a query descending or ascending? In my opinion, the vast majority of the time the desired behavior whether sorting ascending or descending would be NULLS LAST. Instead, we should have to specify NULLS FIRST. 回答1: Actually, with default sort order ( ASCENDING ) NULL values come last . Logic dictates that the sort order be reversed with the DESCENDING keyword, so NULLs come first in this case. But the best part comes last: you can choose which

Preserving ORDER BY in SELECT INTO

别来无恙 提交于 2019-11-26 09:58:36
问题 I have a T-SQL query that takes data from one table and copies it into a new table but only rows meeting a certain condition: SELECT VibeFGEvents.* INTO VibeFGEventsAfterStudyStart FROM VibeFGEvents LEFT OUTER JOIN VibeFGEventsStudyStart ON CHARINDEX(REPLACE(REPLACE(REPLACE(logName, \'MyVibe \', \'\'), \' new laptop\', \'\'), \' old laptop\', \'\'), excelFilename) > 0 AND VibeFGEventsStudyStart.MIN_TitleInstID <= VibeFGEvents.TitleInstID AND VibeFGEventsStudyStart.MIN_WinInstId <=

SQLite and custom order by

拥有回忆 提交于 2019-11-26 09:36:18
问题 I have a table with categories: ID Category \"1\",\"Baking\" \"3\",\"Family\" \"4\",\"Entertaining\" \"5\",\"Children\" \"6\",\"Desserts\" Now I would like to order the result of the select statement to ID Category \"4\",\"Entertaining\" \"3\",\"Family\" \"1\",\"Baking\" \"5\",\"Children\" \"6\",\"Desserts\" for example. In MySQL, you\'d do it like this: SELECT * FROM CATEGORIES ORDER BY FIELD (ID, 4,3,1,5,6); How would you do it in SQLite though? I don\'t have an \"order by\" field. 回答1:

Custom sort logic in OrderBy using LINQ

China☆狼群 提交于 2019-11-26 09:34:30
问题 What would be the right way to sort a list of strings where I want items starting with an underscore \'_\', to be at the bottom of the list, otherwise everything is alphabetical. Right now I\'m doing something like this, autoList.OrderBy(a => a.StartsWith(\"_\") ? \"ZZZZZZ\"+a : a ) 回答1: If you want custom ordering, but don't want to supply a comparer, you can have it - sql style: autoList .OrderBy(a => a.StartsWith("_") ? 2 : 1 ) .ThenBy(a => a); 回答2: I think you need to use OrderBy(Func<>,

Mongo order by length of array

自闭症网瘾萝莉.ら 提交于 2019-11-26 09:24:55
问题 Lets say I have mongo documents like this: Question 1 { answers:[ {content: \'answer1\'}, {content: \'2nd answer\'} ] } Question 2 { answers:[ {content: \'answer1\'}, {content: \'2nd answer\'} {content: \'The third answer\'} ] } Is there a way to order the collection by size of answers? After a little research I saw suggestions of adding another field, that would contain number of answers and use it as a reference but may be there is native way to do it? 回答1: I thought you might be able to

How to use SQL Order By statement to sort results case insensitive?

浪子不回头ぞ 提交于 2019-11-26 09:17:19
问题 I have a SQLite database that I am trying to sort by Alphabetical order. The problem is, SQLite doesn\'t seem to consider A=a during sorting, thus I get results like this: A B C T a b c g I want to get: A a b B C c g T What special SQL thing needs to be done that I don\'t know about? SELECT * FROM NOTES ORDER BY title 回答1: You can also do ORDER BY TITLE COLLATE NOCASE . Edit: If you need to specify ASC or DESC , add this after NOCASE like ORDER BY TITLE COLLATE NOCASE ASC or ORDER BY TITLE

How does sql server sort your data?

大兔子大兔子 提交于 2019-11-26 08:32:43
问题 I was wondering how sql server sorts it\'s data. I noticed that if I have a table that doesn\'t contain the column \"Id\" and you select data without \"ORDER BY\" sql server doesn\'t automatically sort on the primary column. Does anyone know what rule sql server follows to sort it\'s data? 回答1: Although it's good to wonder about how it might be explained that you often see the same order, I'd like to point out that it never a good idea to rely on implicit order caused by the particular

How to update and order by using ms sql

半世苍凉 提交于 2019-11-26 08:14:06
问题 Ideally I want to do this: UPDATE TOP (10) messages SET status=10 WHERE status=0 ORDER BY priority DESC; In English: I want to get the top 10 available (status=0) messages from the DB and lock them (status=10). A message with a higher priority should be gotten first. unfortunately MS SQL doesn\'t allow an order by clause in the update. Anyway how to circumvent this? 回答1: You can do a subquery where you first get the IDs of the top 10 ordered by priority and then update the ones that are on