sql-order-by

How to display records from a table ordered as in the where clause?

好久不见. 提交于 2020-01-11 11:27:10
问题 I am trying to display the records,order as in the where clause.. example: select name from table where name in ('Yaksha','Arun','Naveen'); It displays Arun,Naveen,Yaksha (alphabetical order) I want display it as same order i.e 'Yaksha''Arun','Naveen' how to display this... I am using oracle db. 回答1: Add this ORDER BY at the query's end: order by case name when 'Yaksha' then 1 when 'Arun' then 2 when 'Naveen' then 3 end (There's no other way to get that order. You need an ORDER BY to get a

How to order dynamically created elements based on a custom attribute? [closed]

有些话、适合烂在心里 提交于 2020-01-11 07:53:08
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 8 years ago . This function seems to work fairly well for sorting divs based on ID: JS var div = $("<div id='3.5'>AA</div>"); div_id_after = Math.floor(parseFloat(div.get(0).id)); $('#'+div_id_after).after(div); HTML <div id=

Avoid filesort with INNER JOIN + ORDER BY

限于喜欢 提交于 2020-01-11 05:32:05
问题 I've been reading other posts but I didn't managed to fix my query. Using DESC order the query is x20 times slower, I must improve that. This is the query: SELECT posts.post_id, posts.post_b_id, posts.post_title, posts.post_cont, posts.thumb, posts.post_user, boards.board_title_l, boards.board_title FROM posts INNER JOIN follow ON posts.post_b_id = follow.board_id INNER JOIN boards ON posts.post_b_id = boards.board_id WHERE follow.user_id =1 ORDER BY posts.post_id DESC LIMIT 10 And these are

MYSQL select last 3 rows, order by ASC

我与影子孤独终老i 提交于 2020-01-11 05:14:06
问题 I just want to select the newest 3 comments on a post, and have them ordered in ASC order. This selects the last 3 rows, however I need them in the reverse order: mysql_query(" SELECT * FROM comments WHERE postID='$id' AND state='0' ORDER BY id DESC LIMIT 3") 回答1: You can reverse sort it later. SELECT * FROM (SELECT * FROM comments WHERE postID='$id' AND state='0' ORDER BY id DESC LIMIT 3) t ORDER BY id ASC; 回答2: This can also be done just in PHP, without modifying the SQL query, by simply

apply “ORDER BY” on a “UNION” (Mysql)

笑着哭i 提交于 2020-01-11 04:41:12
问题 Good Day. So, everythign is in the title :) I am looking to merge the result of two requests and order the result together (as in not one after the other). => I was thinking of applying an union and order them. It didn't worked. I looked around like here on Stack or here developpez (!!french website). I try the different exemple, and suggestion, but no success. It seems from what i red that it's because I am working on Mysql. Anyway, here are my attemps, and the results: My original 2

Ordering distinct column values by (first value of) other column in aggregate function

爷,独闯天下 提交于 2020-01-11 00:51:11
问题 I'm trying to order the output order of some distinct aggregated text based on the value of another column with something like: string_agg(DISTINCT sometext, ' ' ORDER BY numval) However, that results in the error: ERROR: in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list I do understand why this is, since the ordering would be "ill-defined" if the numval of two repeated values differs, with that of another lying in-between. Ideally, I would like to order them by

MySQL indices and order

怎甘沉沦 提交于 2020-01-09 04:18:24
问题 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

Order by `updated_at` and `created_at` even if they are NULL

霸气de小男生 提交于 2020-01-07 04:44:04
问题 I have the following table: CREATE TABLE `entries` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `created_at` datetime DEFAULT NULL, `updated_at` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; I want to sort the entries by both updated_at and created_at . But either of them could be NULL . My query: SELECT * FROM `entries` ORDER BY `updated_at` DESC, `created_at` DESC, `id` DESC But this will put every entry which

Postgresql order by returning two different orders

大城市里の小女人 提交于 2020-01-07 03:24:07
问题 I have a PostgreSQL server running on beta and one running locally. On both, I have a table called profile with a column named name of type character varying (255) . I have checked that the dbs have the same values. The weird part is when I do a select on the profile table with order by name asc I am getting different results. So on my local db, profile with name (I)Contractor is first and on beta profile with name 3B is first. So it seems on my local db ( comes before numeric characters and

Same Query returns different results (MySQL Group By)

风流意气都作罢 提交于 2020-01-07 02:50:28
问题 This only happens for queries that force GROUP BY after ORDER BY . Goal: Get latest balance for each unit for the given cardID . Table: cardID | unit | balance | date --------|-----------|-----------|-------------- A1 | DEPOSIT | 100 | 2016-05-01 A1 | DEPOSIT | 90 | 2016-05-02 A1 | DEPOSIT | 80 | 2016-05-03 A1 | DEPOSIT | 75 | 2016-05-04 A1 | MINUTE | 1000 | 2016-05-01 A1 | MINUTE | 900 | 2016-05-02 A1 | MINUTE | 800 | 2016-05-03 Query: SELECT * FROM ( SELECT unit, balance FROM cardBalances