sql-order-by

Order SQL results where each record referencing another record on the same table comes after the referenced record

ⅰ亾dé卋堺 提交于 2021-02-11 14:27:19
问题 I have the following data: id, parent_id 1, 3 2, null 3, 2 4, 2 Where parent_id is a reference to the SAME table. I need to sort these columns so that each record is after its parent (not necessarily immediately after). So I would expect this result: id, parent_id 2, null 3, 2 1, 3 4, 2 I'm guessing there is no clean efficient way to do this without any significant schema changes, but in case anybody can think of a method I'm asking this here. One possible method would be to do multiple

In Postgresql how to order by date while keeping custom date format

给你一囗甜甜゛ 提交于 2021-02-10 08:17:06
问题 The issue is that using to_char will turn order by date into order by ascii. Example: SELECT foo, bar FROM baz ORDER BY foo; I would like to format foo using to_char, but doing this will affect the order: SELECT to_char(foo,'dd/MM/yyyy') as foo, bar FROM baz ORDER BY foo; Because foo now is of type text. There is a way to correctly do this? Or only in the code? 回答1: You can use a different alias for the formatted column: SELECT to_char(foo,'dd/MM/yyyy') as formatted_foo, bar FROM baz ORDER BY

In Postgresql how to order by date while keeping custom date format

孤人 提交于 2021-02-10 08:15:22
问题 The issue is that using to_char will turn order by date into order by ascii. Example: SELECT foo, bar FROM baz ORDER BY foo; I would like to format foo using to_char, but doing this will affect the order: SELECT to_char(foo,'dd/MM/yyyy') as foo, bar FROM baz ORDER BY foo; Because foo now is of type text. There is a way to correctly do this? Or only in the code? 回答1: You can use a different alias for the formatted column: SELECT to_char(foo,'dd/MM/yyyy') as formatted_foo, bar FROM baz ORDER BY

In Postgresql how to order by date while keeping custom date format

送分小仙女□ 提交于 2021-02-10 08:15:18
问题 The issue is that using to_char will turn order by date into order by ascii. Example: SELECT foo, bar FROM baz ORDER BY foo; I would like to format foo using to_char, but doing this will affect the order: SELECT to_char(foo,'dd/MM/yyyy') as foo, bar FROM baz ORDER BY foo; Because foo now is of type text. There is a way to correctly do this? Or only in the code? 回答1: You can use a different alias for the formatted column: SELECT to_char(foo,'dd/MM/yyyy') as formatted_foo, bar FROM baz ORDER BY

In Postgresql how to order by date while keeping custom date format

…衆ロ難τιáo~ 提交于 2021-02-10 08:14:07
问题 The issue is that using to_char will turn order by date into order by ascii. Example: SELECT foo, bar FROM baz ORDER BY foo; I would like to format foo using to_char, but doing this will affect the order: SELECT to_char(foo,'dd/MM/yyyy') as foo, bar FROM baz ORDER BY foo; Because foo now is of type text. There is a way to correctly do this? Or only in the code? 回答1: You can use a different alias for the formatted column: SELECT to_char(foo,'dd/MM/yyyy') as formatted_foo, bar FROM baz ORDER BY

In Postgresql how to order by date while keeping custom date format

我怕爱的太早我们不能终老 提交于 2021-02-10 08:13:15
问题 The issue is that using to_char will turn order by date into order by ascii. Example: SELECT foo, bar FROM baz ORDER BY foo; I would like to format foo using to_char, but doing this will affect the order: SELECT to_char(foo,'dd/MM/yyyy') as foo, bar FROM baz ORDER BY foo; Because foo now is of type text. There is a way to correctly do this? Or only in the code? 回答1: You can use a different alias for the formatted column: SELECT to_char(foo,'dd/MM/yyyy') as formatted_foo, bar FROM baz ORDER BY

In Postgresql how to order by date while keeping custom date format

▼魔方 西西 提交于 2021-02-10 08:13:01
问题 The issue is that using to_char will turn order by date into order by ascii. Example: SELECT foo, bar FROM baz ORDER BY foo; I would like to format foo using to_char, but doing this will affect the order: SELECT to_char(foo,'dd/MM/yyyy') as foo, bar FROM baz ORDER BY foo; Because foo now is of type text. There is a way to correctly do this? Or only in the code? 回答1: You can use a different alias for the formatted column: SELECT to_char(foo,'dd/MM/yyyy') as formatted_foo, bar FROM baz ORDER BY

Dapper Order By

五迷三道 提交于 2021-02-07 14:16:36
问题 Is there any reason why the following code wouldn't be retrieved in the correct order when using dapper? connection.Query<User>("SELECT id, name " + "FROM user " + "ORDER BY @sort @dir " + "LIMIT @offset, @pageSize; ", new { sort = sortOrder, // sortOrder = "name" dir = sortDirection, // sortDirection = "ASC" offset = pageIndex * pageSize, // offset = 0 pageSize = pageSize // pageSize = 10 }); It always returns without applying the ordering. I could just place the sortOrder and sortDirection

SQL - does order of OR conditions matter?

你离开我真会死。 提交于 2021-02-07 09:13:04
问题 I have to SELECT one row which meets condition1 OR condition2 . However, condition1 is preferable. If there are two rows, where the first one meets condition1 (and does not meet condition2) and second meets condition2 (and does not meet condition1 ) then the first one should be returned. So for SQL: SELECT * FROM table WHERE col1 = 1 OR col2 = 5 LIMIT 1 Will it return the row that meets condition col1 = 1 first? Or will it return rows in random order? If it will be random then how to achieve

SQL - does order of OR conditions matter?

橙三吉。 提交于 2021-02-07 09:12:58
问题 I have to SELECT one row which meets condition1 OR condition2 . However, condition1 is preferable. If there are two rows, where the first one meets condition1 (and does not meet condition2) and second meets condition2 (and does not meet condition1 ) then the first one should be returned. So for SQL: SELECT * FROM table WHERE col1 = 1 OR col2 = 5 LIMIT 1 Will it return the row that meets condition col1 = 1 first? Or will it return rows in random order? If it will be random then how to achieve