sql-order-by

MySQL specify arbitrary order by id

被刻印的时光 ゝ 提交于 2019-12-18 04:42:33
问题 Is it possible to specify an arbitrary order for a MySQL SELECT statement? E.g., SELECT * FROM table_name WHERE id IN (1, 3, 2, 9, 7) ORDER BY (1, 3, 2, 9, 7); The order of the numbers listed directly after IN do not seem to matter. 回答1: FIND_IN_SET function will do the trick SELECT * FROM table_name WHERE id IN (1, 3, 2, 9, 7) ORDER BY FIND_IN_SET(id, '1,3,2,9,7'); http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set EDIT: Note the lack of spaces in the string

MySQL ORDER BY [custom SET field value]

…衆ロ難τιáo~ 提交于 2019-12-18 04:24:16
问题 I hope there's a simple solution for this: I have a table where each row has it's own status ( SET type field). Statuses can be: offline available busy distance I'd like to order as follows available busy distance offline I thought a simple ORDER BY `status` ASC will do the trick (alphabetical order) but it gives me the following: offline available busy distance How can is sort out this in the most simple way? Thanks in advance, fabrik 回答1: Thought I'd add another way to order by custom field

Order by day_of_week in MySQL

爷,独闯天下 提交于 2019-12-17 23:49:13
问题 How can I order the mysql result by varchar column that contains day of week name? Note that MONDAY should goes first, not SUNDAY. 回答1: Either redesign the column as suggested by Williham Totland, or do some string parsing to get a date representation. If the column only contains the day of week, then you could do this: ORDER BY FIELD(<fieldname>, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY'); 回答2: Why not this? ORDER BY ( CASE DAYOFWEEK(dateField) WHEN 1 THEN

Combining ORDER BY AND UNION in SQL Server

情到浓时终转凉″ 提交于 2019-12-17 22:38:37
问题 How can I get first record of a table and last record of a table in one result-set? This Query fails SELECT TOP 1 Id,Name FROM Locations ORDER BY Id UNION ALL SELECT TOP 1 Id,Name FROM Locations ORDER BY Id DESC Any help? 回答1: Put your order by and top statements into sub-queries: select first.Id, first.Name from ( select top 1 * from Locations order by Id) first union all select last.Id, last.Name from ( select top 1 * from Locations order by Id desc) last 回答2: select * from ( SELECT TOP 1

SQLite Query in non case sensitive alphabetical order [duplicate]

浪尽此生 提交于 2019-12-17 22:07:41
问题 This question already has answers here : How to use SQL Order By statement to sort results case insensitive? (3 answers) Closed 3 years ago . All I want to do is grab the stuff in alphabetical order and ignore the capital letters. db.rawQuery("SELECT " + catName + " FROM "+tableName+" ORDER BY "+catName+" ASC COLLATE NOCASE;", null); This is the code I'm using above, but it always gives me an SQLite exception saying that COLLATE is a syntax error. android.database.sqlite.SQLiteException: near

Query to ORDER BY the number of rows returned from another SELECT

风流意气都作罢 提交于 2019-12-17 20:32:48
问题 I'm trying to wrap my head around SQL and I need some help figuring out how to do the following query in PostgreSQL 9.3. I have a users table, and a friends table that lists user IDs and the user IDs of friends in multiple rows. I would like to query the user table, and ORDER BY the number of mutual friends in common to a user ID. So, the friends table would look like: user_id | friend_user_id 1 | 4 1 | 5 2 | 10 3 | 7 And so on, so user 1 lists 4 and 5 as friends, and user 2 lists 10 as a

How to change the collation of sqlite3 database to sort case insensitively?

孤者浪人 提交于 2019-12-17 19:32:54
问题 I have a query for sqlite3 database which provides the sorted data. The data are sorted on the basis of a column which is a varchar column "Name". Now when I do the query select * from tableNames Order by Name; It provides the data like this. Pen Stapler pencil Means it is considering the case sensitive stuff. The way I want is as follows Pen pencil Stapler So what changes should I make in sqlite3 database for the necessary results? Related How to set Sqlite3 to be case insensitive when

Order a List (C#) by many fields? [duplicate]

旧城冷巷雨未停 提交于 2019-12-17 17:36:32
问题 This question already has an answer here : Multiple Order By with LINQ [duplicate] (1 answer) Closed 5 years ago . I want to order a List of objects in C# by many fields, not just by one. For example, let's suppose I have a class called X with two Attributes, A and B, and I have the following objects, in that order: object1 => A = "a", B = "h" object2 => A = "a", B = "c" object3 => A = "b", B = "x" object4 => A = "b", B = "b" and I want to order the list by A attribute first, and when they

mysql order by, null first, and DESC after

依然范特西╮ 提交于 2019-12-17 17:32:23
问题 How can I order DESC by a field, but list the NULL values first? So I'm having a table: reuestId | offerId | offerTitle 1 | 1 | Alfa NULL | 2 | Beta 2 | 3 | Gamma I want to select them so that the results would be: NULL | 2 | Beta 2 | 3 | Gamma 1 | 1 | Alfa 回答1: Try this: ORDER BY [reuestId] IS NULL DESC, [reuestId] DESC should work (for mySql) 回答2: SELECT * FROM TableX ORDER BY (requestId IS NOT NULL) , requestId DESC 来源: https://stackoverflow.com/questions/9307613/mysql-order-by-null-first

Laravel Eloquent: Ordering results of all()

拈花ヽ惹草 提交于 2019-12-17 17:25:21
问题 I'm stuck on a simple task. I just need to order results coming from this call $results = Project::all(); Where Project is a model. I've tried this $results = Project::all()->orderBy("name"); But it didn't work. Which is the better way to obtain all data from a table and get them ordered? 回答1: You can actually do this within the query. $results = Project::orderBy('name')->get(); This will return all results with the proper order. 回答2: You could still use sortBy (at the collection level)