sql-order-by

“ORDER BY … USING” clause in PostgreSQL

有些话、适合烂在心里 提交于 2019-11-27 11:30:20
问题 The ORDER BY clause is decribed in the PostgreSQLdocumentation as: ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] Can someone give me some examples how to use the USING operator ? Is it possible to get an alternating order of the resultset? 回答1: A very simple example would be: > SELECT * FROM tab ORDER BY col USING < But this is boring, because this is nothing you can't get with the traditional ORDER BY col ASC . Also the standard catalog doesn't

MySQL indices and order

自作多情 提交于 2019-11-27 11:28:46
This is a question that I've had forever. As far as I know the order of indices matter. So an index like [first_name, last_name] is not the same as [last_name, first_name] , right? If I only define the first index, does it mean that it will only used for SELECT * FROM table WHERE first_name="john" AND last_name="doe"; and not for SELECT * FROM table WHERE last_name="doe" AND first_name="john"; Since I am using a ORM, I have no idea in which order these columns are going to be called. Does that mean that I have to add indices on all permutations? That is doable if I have a 2 column index, but

SQL Error with Order By in Subquery

依然范特西╮ 提交于 2019-11-27 11:25:20
I'm working with SQL Server 2005. My query is: SELECT ( SELECT COUNT(1) FROM Seanslar WHERE MONTH(tarihi) = 4 GROUP BY refKlinik_id ORDER BY refKlinik_id ) as dorduncuay And the error: The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified. How can I use ORDER BY in a sub query? This is the error you get (emphasis mine): The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified. So, how can you

Django Query using .order_by() and .latest()

£可爱£侵袭症+ 提交于 2019-11-27 11:06:28
问题 I have a model: class MyModel(models.Model): creation_date = models.DateTimeField(auto_now_add = True, editable=False) class Meta: get_latest_by = 'creation_date' I had a query in my view that did the following: instances = MyModel.objects.all().order_by('creation_date') And then later I wanted instances.latest() , but it would not give me the correct instance, in fact it gave me the first instance. Only when I set order_by to -creation_date or actually removed the order_by from the query did

SQL for ordering by number - 1,2,3,4 etc instead of 1,10,11,12

妖精的绣舞 提交于 2019-11-27 10:47:04
I’m attempting to order by a number column in my database which has values 1-999 When I use ORDER_BY registration_no ASC I get…. 1 101 102 103 104 105 106 107 108 109 11 110 Etc… So it appears to be ordering by the first digit as oppose to the number. Does anyone know what SQL to use if I want to order this by value? So 1,2,3,4,5,6 etc Gordon Linoff One way to order by positive integers, when they are stored as varchar , is to order by the length first and then the value: order by len(registration_no), registration_no This is particularly useful when the column might contain non-numeric values

SQLite ORDER BY string containing number starting with 0

喜夏-厌秋 提交于 2019-11-27 10:46:43
问题 as the title states: I have a select query, which I'm trying to "order by" a field which contains numbers, the thing is this numbers are really strings starting with 0s, so the "order by" is doing this... ... 10 11 12 01 02 03 ... Any thoughts? EDIT: if I do this: "...ORDER BY (field+1)" I can workaround this, because somehow the string is internally being converted to integer. Is this the a way to "officially" convert it like C's atoi? 回答1: You can use CAST http://www.sqlite.org/lang_expr

How to order by more than one field in Grails?

99封情书 提交于 2019-11-27 10:44:08
问题 Is there a way to get a list ordered by two fields, say last and first names? I know .listOrderByLastAndFirst and .list(sort:'last, first') won't work. 回答1: This old solution no longer works. Please see mattlary's answer below You may have to write a custom finder in HQL or use the Criteria Builder. MyDomain.find("from Domain as d order by last,first desc") Or def c = MyDomain.createCriteria() def results = c.list { order("last,first", "desc") } 回答2: Hates_ criteria answer didn't seem to work

Sort CSV file by column priority using the “sort” command

不打扰是莪最后的温柔 提交于 2019-11-27 09:34:24
问题 I have a csv file, and I would like to sort it by column priority, like "order by". For example: 3;1;2 1;3;2 1;2;3 2;3;1 2;1;3 3;2;1 If this situation was the result of a "select", the "order by" would be as follows: order by column2, column1, column3 - the result would be: 2;1;3 3;1;2 1;2;3 3;2;1 1;3;2 2;3;1 I'd like to know how to get this same result using "sort" command on Unix. 回答1: sort --field-separator=';' --key=2,1,3 回答2: Charlie's answer above didn't work for me on Cygwin (sort

Mysql delete statement with limit

强颜欢笑 提交于 2019-11-27 09:24:13
I'm trying to delete rows from a table but I get an error. DELETE FROM `chat_messages` ORDER BY `timestamp` DESC LIMIT 20, 50; I get this error at 50: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 50' at line 1 No idea what's wrong. You cannot specify offset in DELETE 's LIMIT clause. So the only way to do that is to rewrite your query to something like: DELETE FROM `chat_messages` WHERE id IN (select id from (select id FROM `chat_messages` ORDER BY `timestamp` DESC LIMIT 20, 50) x) Supposing that you

Display MySQL results by date

ε祈祈猫儿з 提交于 2019-11-27 09:23:32
I have this mysql Table: +--------------------+---------+-------+ | date | query | count | |--------------------+---------+-------| |2012-11-18 09:52:00 | Michael | 1 | |2012-11-18 10:47:10 | Tom | 2 | |2012-11-17 15:02:12 | John | 1 | |2012-11-17 22:52:10 | Erik | 3 | |2012-11-16 09:42:01 | Larry | 1 | |2012-11-16 07:41:33 | Kate | 1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ and so on. I can simply take results and order them by date in one row via this code: $queries = mysql_query("SELECT * FROM my_tables ORDER BY date DESC LIMIT 20"); while($row = mysql_fetch_array($queries)){ echo "Name "