sql-order-by

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

廉价感情. 提交于 2019-11-28 10:49:53
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 string comparing? Dror Harari The SQLite Datatypes documentation discusses user-defined collation sequences.

How to sort results order by timestamp except one of them?

最后都变了- 提交于 2019-11-28 10:26:32
问题 I have a table like this: // Mytable +----+--------------------+------+------------------+-----------+ | Id | QuestionOrAnswer | Type | AcceptedAnswerId | timestamp | +----+--------------------+------+------------------+-----------+ | 1 | question1 | 0 | 3 | 1 | | 2 | answer1 | 1 | NULL | 2 | | 3 | answer2 | 1 | NULL | 3 | -- accepted answer | 4 | answer3 | 1 | NULL | 4 | +----+--------------------+------+------------------+-----------+ Now I want this result: (please focus on the order) +---

Need a sequence number for every row in MySQL query

你。 提交于 2019-11-28 10:05:36
问题 So I found this great use: SELECT (@row:=@row+1) AS ROW, ID FROM TableA ,(SELECT @row := 0) r ORDER BY ID DESC The @row:=@row+1 works great, but I get the row ordered by the ID. My tables look more like this: SELECT (@row:=@row+1) AS ROW, ID , ColA, ColB, ColC FROM TableA JOIN TableB on TableB.ID = TableA.ID JOIN TableC on TableC.ID = TableA.ID WHERE ID<500 ,(SELECT @row := 0) r ORDER BY ID DESC Note: I noticed that if I remove the JOINs I DO get the requested result (In Which ROW is the

Sorting a MySQL query with ORDER BY or with PHP sort functions

南楼画角 提交于 2019-11-28 10:04:33
问题 I have a query that I want to sort alphabetically, but the trick is that I want the sorting to treat two columns equally. For instance, if the first row of first_col equals apple and the second row of second_col equals aardvark I want the value in the second row of second_col to be listed before the value in the first row of first_col . A value (not NULL or '' ) will always exist in every row of second_col , but the value in first_col can be '' . Hopefully I have explained this good enough. I

Rails Active query order by multiple values in specific order?

送分小仙女□ 提交于 2019-11-28 09:54:37
问题 I have a table Roles(id int,,name vachar(50),role enum(CAD,CA,CM,CV )) I want select data that order by specific values in specific order . My active model query: role.order('role asc') then the result: 1 name1 CAD 2 name2 CA 3 name2 CM But I want the result like: 1 name1 CAD 2 name2 CM 3 name2 CA Can anyone help me? Thanks in advance 回答1: A portable solution would be to use a CASE statement as an inlined map in your ORDER BY: query.order(%q{ case role when 'CAD' then 1 when 'CM' then 2 when

What is the MS SQL Server capability similar to the MySQL FIELD() function?

独自空忆成欢 提交于 2019-11-28 09:09:22
MySQL provides a string function named FIELD() which accepts a variable number of arguments. The return value is the location of the first argument in the list of the remaining ones. In other words: FIELD('d', 'a', 'b', 'c', 'd', 'e', 'f') would return 4 since 'd' is the fourth argument following the first. This function provides the capability to sort a query's results based on a very specific ordering. For my current application there are four statuses that I need to manager: active, approved, rejected, and submitted. However, if I simply order by the status column, I feel the usability of

LINQ multiple order by

你。 提交于 2019-11-28 08:44:29
问题 I have created a function that has the follwing parameter: List<Expression<Func<CatalogProduct, bool>>> orderBy = null This parameter is optional, If it is filled it should create a order by and than by constuction for me, so that I can order the result on the SQL server. I tried: IOrderedQueryable temp = null; foreach (Expression<Func<CatalogProduct, bool>> func in orderBy) { if (temp == null) { temp = catalogProducts.OrderBy(func); } else { temp = temp.ThanBy(func); } } But the than By is

Indexed ORDER BY with LIMIT 1

余生颓废 提交于 2019-11-28 07:58:03
问题 I'm trying to fetch most recent row in a table. I have a simple timestamp created_at which is indexed. When I query ORDER BY created_at DESC LIMIT 1 , it takes far more than I think it should (about 50ms on my machine on 36k rows). EXPLAIN -ing claims that it uses backwards index scan , but I confirmed that changing the index to be (created_at DESC) does not change the cost in query planner for a simple index scan . How can I optimize this use case? Running postgresql 9.2.4 . Edit: # EXPLAIN

How to reverse the default ordering in Mysql?

旧时模样 提交于 2019-11-28 07:36:48
问题 In Mysql, when you execute a select SQL statement, there is a default ordering if you don't include a sorting clause, how to reverse the default ordering? Just add DESC ? 回答1: If you want the data to come out consistently ordered, you have to use ORDER BY followed by the column(s) you want to order the query by. ASC is the default, so you don't need to specify it. IE: ORDER BY your_column ...is the equivalent to: ORDER BY your_column ASC ASC/DESC is on a per column basis. For example: ORDER

Which row's fields are returned when Grouping with MySQL?

主宰稳场 提交于 2019-11-28 07:32:27
问题 I have a MySQL table with the fields id and string . id s are unique. string s are varchars and are non-unique. I perform the following query: SELECT id, string, COUNT( * ) AS frequency FROM table GROUP BY string ORDER BY frequency DESC, id ASC Questions Assume the table contains three rows with identical string values, and id s 1, 2, and 3. Which id is going to be returned ( 1, 2, or 3 )? Which id is this query going to ORDER BY ( Same as is returned? ... see question 1 )? Can you control