sql-order-by

MySQL order by “best match”

孤街醉人 提交于 2019-11-26 20:35:09
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'd like it this way: hab it (first because 'hab' is the beginning) alp hab et (second because 'hab' is in

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

随声附和 提交于 2019-11-26 20:28:22
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 as long it is not after last row. rankResult = rankResult + c.getString(iRow) + "\n"; //Returning value

How to get two random records with Django

耗尽温柔 提交于 2019-11-26 19:51:11
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. 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. 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 in an ORDER BY RAND() SQL query. As an example, here's how mysql handles that (the situation is not much different

TSQL - Is it possible to define the sort order?

不打扰是莪最后的温柔 提交于 2019-11-26 19:36:16
问题 Is it possible to define a sort order for the returned results? I would like the sort order to be 'orange' 'apple' 'strawberry' not ascending or descending. I know ORDER BY can do ASC or DESC but is there a DEFINED('orange', 'apple', 'strawberry') type thing? This will be running on SQL Server 2000. 回答1: It's incredibly clunky, but you can use a CASE statement for ordering: SELECT * FROM Blah ORDER BY CASE MyColumn WHEN 'orange' THEN 1 WHEN 'apple' THEN 2 WHEN 'strawberry' THEN 3 END

SQL (ORACLE): ORDER BY and LIMIT

淺唱寂寞╮ 提交于 2019-11-26 18:53:37
I want do sorting by property ALL data in my db and ONLY AFTER that use LIMIT and OFFSET. Query like this: SELECT select_list FROM table_expression [ ORDER BY ... ] [ LIMIT { number | ALL } ] [ OFFSET number ] I know the sorting ends as soon as it has found the first row_count rows of the sorted result. Can I do sorting all data before calling LIMIT and OFFSET? Justin Cave Prior to 12.1, Oracle does not support the LIMIT or OFFSET keywords. If you want to retrieve rows N through M of a result set, you'd need something like: SELECT a.* FROM (SELECT b.*, rownum b_rownum FROM (SELECT c.* FROM

MySQL Order before Group by

℡╲_俬逩灬. 提交于 2019-11-26 18:47:18
I need to find the latest post for each author and then group the results so I only a single latest post for each author. SELECT wp_posts.* FROM wp_posts WHERE wp_posts.post_status='publish' AND wp_posts.post_type='post' GROUP BY wp_posts.post_author ORDER BY wp_posts.post_date DESC This is correctly grouping the output so I only get one post per author, but it is ordering the results after they have been grouped and not before they have been selected. edze select wp_posts.* from wp_posts where wp_posts.post_status='publish'and wp_posts.post_type='post' group by wp_posts.post_author having wp

sql ORDER BY multiple values in specific order?

混江龙づ霸主 提交于 2019-11-26 18:33:19
Ok I have a table with a indexed key and a non indexed field. I need to find all records with a certain value and return the row. I would like to know if I can order by multiple values. Example: id x_field -- ----- 123 a 124 a 125 a 126 b 127 f 128 b 129 a 130 x 131 x 132 b 133 p 134 p 135 i pseudo: would like the results to be ordered like this, where ORDER BY x_field = 'f', 'p', 'i', 'a' SELECT * FROM table WHERE id NOT IN (126) ORDER BY x_field 'f', 'p', 'i', 'a' So the results would be: id x_field -- ----- 127 f 133 p 134 p 135 i 123 a 124 a 125 a 129 a The syntax is valid but when I

ActiveRecord Find All not sorting by ID?

谁都会走 提交于 2019-11-26 18:25:46
问题 I've got a strange issue on a Heroku deployment that I can't seem to duplicate locally. Basically when I find all on a specific model instead of sorting by ID it seems to return them in no order at all. Typically the records come out like so: >> Model.all => [<model id: 2>,<model id: 1>,<model id: 3>,<model id: 4>,<model id: 5>] ... and so on. If I explicitly call Model.order("id ASC") it returns the models as expected. What gives? Why would find all not return the objects in descending ID

How do I return rows with a specific value first?

我怕爱的太早我们不能终老 提交于 2019-11-26 18:15:30
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 using only one query? On SQL Server, Oracle, DB2, and many other database systems, this is what you can

Mysql delete statement with limit

旧巷老猫 提交于 2019-11-26 17:49:50
问题 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. 回答1: 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