sql-order-by

MySQL ORDER BY IN()

随声附和 提交于 2019-11-26 08:05:24
问题 I have a PHP array with numbers of ID\'s in it. These numbers are already ordered. Now i would like to get my result via the IN() method, to get all of the ID\'s. However, these ID\'s should be ordered like in the IN method. For example: IN(4,7,3,8,9) Should give a result like: 4 - Article 4 7 - Article 7 3 - Article 3 8 - Article 8 9 - Article 9 Any suggestions? Maybe there is a function to do this? Thanks! 回答1: I think you may be looking for function FIELD -- while normally thought of as a

SQL Server UNION - What is the default ORDER BY Behaviour

陌路散爱 提交于 2019-11-26 07:46:18
问题 If I have a few UNION Statements as a contrived example: SELECT * FROM xxx WHERE z = 1 UNION SELECT * FROM xxx WHERE z = 2 UNION SELECT * FROM xxx WHERE z = 3 What is the default order by behaviour? The test data I\'m seeing essentially does not return the data in the order that is specified above. I.e. the data is ordered, but I wanted to know what are the rules of precedence on this. Another thing is that in this case xxx is a View. The view joins 3 different tables together to return the

MySQL order by “best match”

邮差的信 提交于 2019-11-26 07:38:07
问题 I have a table that contains words and an input field to search that table using a live search. Currently, I use the following query to search the table: SELECT word FROM words WHERE word LIKE \'%searchstring%\' ORDER BY word ASC Is there a way to order the results so that the ones where the string is found at the beginning of the word come first and those where the string appears later in the word come last? An example: searching for \' hab \' currently returns a lphabet h abit r ehab but I\

How do i order my SQLITE database in descending order, for an android app?

痴心易碎 提交于 2019-11-26 07:37:48
问题 What is the most efficient method of showing my data in descending order? public String getRank() { String[] rank = new String[]{ KEY_ROWID }; Cursor c = scoreDb.query(DATABASE_TABLE, rank, null, null, null, null, null); //reading information from db. String rankResult = \"\"; int iRow = c.getColumnIndex(KEY_ROWID); //Cursor looking for column setting equal to these ints. for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { //Move to first row - where cursor starts and moves to next row

How to get two random records with Django

你离开我真会死。 提交于 2019-11-26 07:27:34
问题 How do I get two distinct random records using Django? I\'ve seen questions about how to get one but I need to get two random records and they must differ. 回答1: If you specify the random operator in the ORM I'm pretty sure it will give you two distinct random results won't it? MyModel.objects.order_by('?')[:2] # 2 random results. 回答2: The order_by('?')[:2] solution suggested by other answers is actually an extraordinarily bad thing to do for tables that have large numbers of rows. It results

MySQL: Sort GROUP_CONCAT values

别等时光非礼了梦想. 提交于 2019-11-26 07:00:28
问题 In short: Is there any way to sort the values in a GROUP_CONCAT statement? Query: GROUP_CONCAT((SELECT GROUP_CONCAT(parent.name SEPARATOR \" » \") FROM test_competence AS node, test_competence AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.id = l.competence AND parent.id != 1 ORDER BY parent.lft) SEPARATOR \"<br />\\n\") AS competences I get this row: Crafts » Joinery Administration » Organization I want it like this: Administration » Organization Crafts » Joinery 回答1:

MySQL - ORDER BY values within IN()

别等时光非礼了梦想. 提交于 2019-11-26 07:00:15
问题 I\'m hoping to sort the items returned in the following query by the order they\'re entered into the IN() function . INPUT: SELECT id, name FROM mytable WHERE name IN (\'B\', \'A\', \'D\', \'E\', \'C\'); OUTPUT: | id | name | ^--------^---------^ | 5 | B | | 6 | B | | 1 | D | | 15 | E | | 17 | E | | 9 | C | | 18 | C | Any ideas? 回答1: SELECT id, name FROM mytable WHERE name IN ('B', 'A', 'D', 'E', 'C') ORDER BY FIELD(name, 'B', 'A', 'D', 'E', 'C') The FIELD function returns the position of the

Order a MySQL table by two columns

别说谁变了你拦得住时间么 提交于 2019-11-26 06:55:51
How do I sort a MySQL table by two columns? What I want are articles sorted by highest ratings first, then most recent date. As an example, this would be a sample output (left # is the rating, then the article title, then the article date) 50 | This article rocks | Feb 4, 2009 35 | This article is pretty good | Feb 1, 2009 5 | This Article isn't so hot | Jan 25, 2009 The relevant SQL I'm using is: ORDER BY article_rating, article_time DESC I can sort by one or the other, but not both. Default sorting is ascending, you need to add the keyword DESC to both your orders: ORDER BY article_rating

LINQ Orderby Descending Query

[亡魂溺海] 提交于 2019-11-26 06:55:51
问题 I\'m sure this will be a relatively simple one. I have a LINQ query that I want to order by the most recently created date. See: var itemList = from t in ctn.Items where !t.Items && t.DeliverySelection orderby t.Delivery.SubmissionDate descending select t; I have also tried: var itemList = (from t in ctn.Items where !t.Items && t.DeliverySelection select t).OrderByDescending(); but this gives an error : No overload for method \'OrderByDescending\' takes 0 arguments From what I\'ve read, I\'m

How do I return rows with a specific value first?

ぃ、小莉子 提交于 2019-11-26 06:16:07
问题 I want my query to return the rows of the table where a column contains a specific value first, and then return the rest of the rows alphabetized. If I have a table something like this example: - Table: Users - id - name - city - 1 George Seattle - 2 Sam Miami - 3 John New York - 4 Amy New York - 5 Eric Chicago - 6 Nick New York And using that table I want to my query to return the rows which contain New York first, and then the rest of the rows alphabetized by city. Is this possible to do