sql-order-by

Select last n rows without use of order by clause

孤街醉人 提交于 2019-12-01 06:04:13
I want to fetch the last n rows from a table in a Postgres database. I don't want to use an ORDER BY clause as I want to have a generic query. Anyone has any suggestions? A single query will be appreciated as I don't want to use FETCH cursor of Postgres. That you get what you expect with Lukas' solution (as of Nov. 1st, 2011) is pure luck . There is no "natural order" in an RDBMS by definition. You depend on implementation details that could change with a new release without notice. Or a dump / restore could change that order. It can even change out of the blue when db statistics change and

Is there a [straightforward] way to order results *first*, *then* group by another column, with SQL?

十年热恋 提交于 2019-12-01 06:01:56
I see that in SQL, the GROUP BY has to precede ORDER BY expression. Does this imply that ordering is done after grouping discards identical rows/columns? Because I seem to need to order rows by a timestamp column A first, THEN discarding rows with identical value in column A. Not sure how to accomplish this... I am using MySQL 5.1.41 create table ( A int, B timestamp ) The data could be: +-----+-----------------------+ | A | B | +-----+-----------------------+ | 1 | today | | 1 | yesterday | | 2 | yesterday | | 2 | tomorrow | +-----+-----------------------+ The results I am aiming for would be

Avoid filesort with INNER JOIN + ORDER BY

邮差的信 提交于 2019-12-01 05:58:58
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 the tables (Updated): CREATE TABLE IF NOT EXISTS `posts` ( `post_id` int(11) NOT NULL AUTO_INCREMENT,

MYSQL select last 3 rows, order by ASC

一曲冷凌霜 提交于 2019-12-01 05:47:58
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") 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; This can also be done just in PHP, without modifying the SQL query, by simply iterating backwards through the result set: $res = mysql_query(...); for($i=mysql_num_rows($res)-1; $i>=0; $i--) {

MySQL sorting multiple columns with different sort order

雨燕双飞 提交于 2019-12-01 03:51:21
问题 I have a table in which I have three fields with data type date , int and bigint . I want to sort my select query using all these three columns. I want to sort them all in descending order. For example: Select * From mytbl order by date desc,intnum desc, bigintnum desc; Is it possible that i could get a result starting from max of all three columns. like latest date, highest intnum and higest bigintnum. 回答1: no What your query does is get the max date , followed by the max intnum of the max

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

无人久伴 提交于 2019-12-01 03:20:49
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 requests SELECT * FROM user_relation WHERE from_user_id = 1 List item SELECT * FROM user_relation WHERE to

How does order by clause works if two values are equal?

断了今生、忘了曾经 提交于 2019-12-01 03:18:15
This is my NEWSPAPER table. National News A 1 Sports D 1 Editorials A 12 Business E 1 Weather C 2 Television B 7 Births F 7 Classified F 8 Modern Life B 1 Comics C 4 Movies B 4 Bridge B 2 Obituaries F 6 Doctor Is In F 6 When i run this query select feature,section,page from NEWSPAPER where section = 'F' order by page; It gives this output Doctor Is In F 6 Obituaries F 6 Births F 7 Classified F 8 But in Kevin Loney's Oracle 10g Complete Reference the output is like this Obituaries F 6 Doctor Is In F 6 Births F 7 Classified F 8 Please help me understand how is it happening? In relational

ORDER BY with Case-Statement DESC

心已入冬 提交于 2019-12-01 02:53:00
问题 How to ORDER BY with a CASE -Statement first group: null values in date-column Col1 sorted by date-column Col2 DESC second group: not-null values in date-column- Col1 sorted by Col1 DESC I've tried following: SELECT columns FROM tables WHERE condition ORDER BY case when Table1.Col1 IS NULL then 0 end, Table2.Col2 DESC, case when Table1.Col1 IS NOT NULL then 1 end, Table1.Col1 DESC But the sort order is wrong, the NOT NULL values are first(sorted by Col2 instead of Col1). I think i've missed a

Doctrine DQL dynamic ORDER BY parameter

被刻印的时光 ゝ 提交于 2019-12-01 00:50:41
Im trying to pass the ORDER BY column as a parameter in DQL, like below: $this->em->createQuery("SELECT t FROM Entities\Topic t ORDER BY :order") ->setParameters( array('order' => 't.name') )->getResult(); I guess it doesn't work because setParameter will escape :order, however the below solution doesn't seem very good: $order = 't.name'; // Dynamic value $this->em->createQuery("SELECT t FROM Entities\Topic t ORDER BY $order") ->getResult(); Is there a better way to solve this? In that case use Doctrines Querybuilder : $order = 't.name'; // Dynamic value $qb = $this->_em->createQueryBuilder();

Controlling the sibling order under recursive CTE?

时光怂恿深爱的人放手 提交于 2019-11-30 23:35:48
I have a CTE query which looks for main leafs and sub leafs. but I'm having trouble controling the leaf selection order between 2 siblings : Each row in the table is declared as : (childID INT ,parentID INT ,NAME NVARCHAR(30),location int) Where location is a priority to sort IFF they are siblings. And so I have this tree structure : those pairs has a location priority : For example : `a` ( location=1) should be before `f` (location=2) `b` ( location=1) should be before `e` (location=2) `d` ( location=1) should be **before** `c` (location=2) The problem is that it seems that I must first order