sql-order-by

MySQL: Order by field size/length

我是研究僧i 提交于 2019-11-27 00:49:07
问题 Here is a table structure (e.g. test): __________________________________________ | Field Name | Data Type | |________________|_________________________| | id | BIGINT (20) | |________________|_________________________| | title | varchar(25) | |________________|_________________________| | description | text | |________________|_________________________| A query like: SELECT * FROM TEST ORDER BY description DESC; But I would like to order by the field size/length of the field description. The

MySQL sort order by array value

一笑奈何 提交于 2019-11-27 00:44:01
问题 I need to run a MySQL query where the order is determined by an array value. My array is variable but the values in the array correspond to a field in my DB table called 'ID' so I want the result to be returned in the ID order 9, 1, 4. Array ( [0] => 9 [1] => 1 [2] => 4 ) Is this possible in MySQL or would it be possible to sort the MySQL $result using the array after? You can assume the only values being returned are those in the array. 回答1: ORDER BY field(id, 9, 1, 4); http://dev.mysql.com

Mongo order by length of array

徘徊边缘 提交于 2019-11-27 00:41:47
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? I thought you might be able to use $size , but that's only to find arrays of a certain size, not ordering. From the mongo documentation: http://www

Mysql: Order by like?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 00:27:57
assume that we are performing search using keywords: keyword1, keyword2, keyword3 there are records in database with column "name": 1: John Doe 2: Samuel Doe 3: John Smith 4: Anna Smith now Query: SELECT * FROM users WHERE (name LIKE "%John%" OR name LIKE "%Doe%") it will select records: 1,2,3 (in this order) but i want to order it by keyword in example keyword1=John, keyword2=Doe so it should be listed by keywords: 1,3,2 (because i want to perform search for "Doe" after searching for "John") I was thinking about SELECT DISTINCT FROM (...... UNION .....) but it will be much easier to order it

Django : Order by position ignoring NULL

我的未来我决定 提交于 2019-11-27 00:14:15
问题 I have a problem with django queryset ordering. My model contains a field named position (a PositiveSmallIntegerField ), which I'd like to used to order query results. I use order_by('position') , which works great. Problem : my position field is nullable ( null=True, blank=True ), because I don't wan't to specify a position for every 50000 instances of my model :( When some instances have a NULL "position", order_by returns them in the top of the list : I'd like them to be at the end... In

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

大兔子大兔子 提交于 2019-11-26 23:54:00
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 dan04 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 COLLATE NOCASE DESC You can just convert everything to lowercase for the purposes of sorting: SELECT *

MYSQL - Order timestamp values ascending in order, from newest to oldest?

家住魔仙堡 提交于 2019-11-26 23:35:44
问题 I have come across a problem when trying to order certain results by their timestamp value. I would like these results displayed from the newest, to the oldest based on the timestamp values. So to explain this, imagine that there were 3 results: 2012-07-11 17:34:57 2012-07-11 17:33:28 2012-07-11 17:33:07 This result set would be what I would require, but given the following query SELECT timestamp FROM randomTable ORDER BY timestamp ASC I get: 2012-07-11 17:34:57 2012-07-11 17:33:07 2012-07-11

OrderBy and OrderByDescending are stable?

吃可爱长大的小学妹 提交于 2019-11-26 23:18:47
问题 I am currently reading Pro LINQ c# 2008, and in page 87 the guy says OrderBy and OrderByDescending are stable. But he says exactly the opposite in page 96. It looks to me as he is referring to exactly the same functions, so I don't get it. Are they stable or not? 回答1: Yes, they're definitely stable. I picked up the same error in my review of the book. Joe responded to that bit of my review with this: Just for your reader's knowledge, the ordering is now specified to be stable. Initially it

ORDER BY “ENUM field” in MYSQL

可紊 提交于 2019-11-26 22:45:27
问题 There is a field 'noticeBy' enum('email','mobile','all','auto','nothing') NOT NULL DEFAULT 'auto'. As it known ordering by ENUM field performs relative to its index. However, how it possible make order by its values? 回答1: As documented under Sorting: ENUM values are sorted based on their index numbers, which depend on the order in which the enumeration members were listed in the column specification. For example, 'b' sorts before 'a' for ENUM('b', 'a') . The empty string sorts before nonempty

Hibernate Named Query Order By parameter

社会主义新天地 提交于 2019-11-26 22:24:42
Hi can anyone point me to how we can pass an order by clause as named parameter to hql Ex: Works: select tb from TransportBooking as tb and TIMESTAMP(tb.bookingDate, tb.bookingTime) >= current_timestamp() order by tb.bookingDate Does not work: select tb from TransportBooking as tb and TIMESTAMP(tb.bookingDate, tb.bookingTime) >= current_timestamp() order by :order Not supported, input parameters are only allowed in the WHERE and HAVING clauses and you cannot use parameters for the ORDER BY clause. Or if I rephrase, you can't use parameters for columns, only values. So, either: Have as much