sql-order-by

Display MySQL results by date

怎甘沉沦 提交于 2019-11-26 14:40:11
问题 I have this mysql Table: +--------------------+---------+-------+ | date | query | count | |--------------------+---------+-------| |2012-11-18 09:52:00 | Michael | 1 | |2012-11-18 10:47:10 | Tom | 2 | |2012-11-17 15:02:12 | John | 1 | |2012-11-17 22:52:10 | Erik | 3 | |2012-11-16 09:42:01 | Larry | 1 | |2012-11-16 07:41:33 | Kate | 1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ and so on. I can simply take results and order them by date in one row via this code: $queries = mysql_query("SELECT

Select the 3 most recent records where the values of one column are distinct

筅森魡賤 提交于 2019-11-26 14:28:44
I have the following table: id time text otheridentifier ------------------------------------------- 1 6 apple 4 2 7 orange 4 3 8 banana 3 4 9 pear 3 5 10 grape 2 What I want to do is select the 3 most recent records (by time desc), whose otheridentifier s are distinct. So in this case, the result would be id 's: 5, 4, and 2. id = 3 would be skipped because there's a more recent record with the same otheridentifier field. Here's what I tried to do: SELECT * FROM `table` GROUP BY (`otheridentifier`) ORDER BY `time` DESC LIMIT 3 However, I end up getting rows of id = 5, 3 , and 1 instead of 5, 4

SQL how to make null values come last when sorting ascending

吃可爱长大的小学妹 提交于 2019-11-26 14:01:19
I have a SQL table with a datetime field. The field in question can be null. I have a query and I want the results sorted ascendingly by the datetime field, however I want rows where the datetime field is null at the end of the list, not at the beginning. Is there a simple way to accomplish that? select MyDate from MyTable order by case when MyDate is null then 1 else 0 end, MyDate (A "bit" late, but this hasn't been mentioned at all) You didn't specify your DBMS. In standard SQL (and most modern DBMS like Oracle, PostgreSQL, DB2, Firebird, Apache Derby, HSQLDB and H2) you can specify NULLS

Slow query when using ORDER BY

为君一笑 提交于 2019-11-26 13:28:51
Here's the query (the largest table has about 40,000 rows) SELECT Course.CourseID, Course.Description, UserCourse.UserID, UserCourse.TimeAllowed, UserCourse.CreatedOn, UserCourse.PassedOn, UserCourse.IssuedOn, C.LessonCnt FROM UserCourse INNER JOIN Course USING(CourseID) INNER JOIN ( SELECT CourseID, COUNT(*) AS LessonCnt FROM CourseSection GROUP BY CourseID ) C USING(CourseID) WHERE UserCourse.UserID = 8810 If I run this, it executes very quickly (.05 seconds roughly). It returns 13 rows. When I add an ORDER BY clause at the end of the query (ordering by any column) the query takes about 10

Getting row number for query

半腔热情 提交于 2019-11-26 13:20:55
问题 I have a query which will return one row. Is there any way I can find the row index of the row I'm querying when the table is sorted? I've tried rowid but got #582 when I was expecting row #7. Eg: CategoryID Name I9GDS720K4 CatA LPQTOR25XR CatB EOQ215FT5_ CatC K2OCS31WTM CatD JV5FIYY4XC CatE --> C_L7761O2U CatF <-- I want this row (#5) OU3XC6T19K CatG L9YKCYAYMG CatH XKWMQ7HREG CatI I've tried rowid with unexpected results: SELECT rowid FROM Categories WHERE CategoryID = 'C_L7761O2U ORDER BY

The order of a SQL Select statement without Order By clause

五迷三道 提交于 2019-11-26 13:06:36
As I know, from the relational database theory, a select statement without an order by clause should be considered has not particular order. But actually in SQL Server and Oracle (I've tested on those 2 platforms), if I query from a table without an order by clause multiple times, I always get the results in the same order. Does this behavior can be relied on? Anyone can help to explain a little? SingleNegationElimination No, that behavior cannot be relied on. The order is determined by the way the query planner has decided to build up the result set. simple queries like select * from foo

SQL Order By Count

点点圈 提交于 2019-11-26 13:01:03
问题 If I have a table and data like this: ID | Name | Group 1 Apple A 2 Boy A 3 Cat B 4 Dog C 5 Elep C 6 Fish C and I wish to order it according to the total of Group from smallest to largest value, such as : A - 2 records , B - 1 record , C - 3 records , so it will become: 3 Cat B 1 Apple A 2 Boy A 4 Dog C 5 Elep C 6 Fish C I tried $sql = \"SELECT ID,Name FROM table ORDER BY COUNT(Group)\"; but it just returns one result for me. Are there any hints? Thank you. 回答1: You need to aggregate the data

Sorting null values after all others, except special

百般思念 提交于 2019-11-26 12:34:11
问题 I have a PostgreSQL table of items with an optional ordering field: CREATE TABLE tasks ( id integer PRIMARY KEY DEFAULT nextval(\'f_seq\'), f_id integer REFERENCES fixins, name text NOT NULL, sort integer ); I want tasks that have no sort value to sort after all others, with one exception: if sort = -1 I want it to sort after those. So, for example, given these values: id | f_id | name | sort ---+------+----------+------- 1 | 1 | zeta | -1 2 | 1 | alpha | 1 3 | 1 | gamma | 3 4 | 1 | beta | 2

What is MySQL row order for “SELECT * FROM table_name;”?

和自甴很熟 提交于 2019-11-26 12:27:48
Assume that the following query is issued to a MySQL database: SELECT * FROM table_name; Note that no ORDER BY clause is given. My question is: Does MySQL give any guarantees to which order the result set rows will be given? More specifically, can I assume that the rows will be returned in insertion order?, that is the same order in which the rows were inserted into the table. No, there are no guarantees. Unless you specify an order using an ORDER BY clause, the order is totally dependent on internal implementation details. I.e. whatever is most convenient for the RDBMS engine. In practice,

Hibernate Named Query Order By parameter

故事扮演 提交于 2019-11-26 12:26:40
问题 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 回答1: Not supported, input parameters are only allowed in the WHERE and HAVING clauses and you cannot use parameters for the ORDER