sql-order-by

PHP and MySQL: Order by most recent date and limit 10

不羁岁月 提交于 2019-12-17 16:55:25
问题 I am building a notes system on my site and I've got to the stage where users can post notes into the MySQL database using PHP and then PHP prints them out on a page. However, when they print/echo out, the oldest one appears first but I want the most recent first. I also want them to be limited to 10, so only 10 appear on the page. Here is my PHP code, your help will be much appreciated: // initialize some variables $notedisplaylist = ""; $myObject = ""; $result = mysql_query("SELECT * FROM

PostgreSQL ORDER BY issue - natural sort

孤街醉人 提交于 2019-12-17 16:34:16
问题 I've got a Postgres ORDER BY issue with the following table: em_code name EM001 AAA EM999 BBB EM1000 CCC To insert a new record to the table, I select the last record with SELECT * FROM employees ORDER BY em_code DESC Strip alphabets from em_code usiging reg exp and store in ec_alpha Cast the remating part to integer ec_num Increment by one ec_num++ Pad with sufficient zeors and prefix ec_alpha again When em_code reaches EM1000, the above algorithm fails. First step will return EM999 instead

Varchar to number conversion for sorting

扶醉桌前 提交于 2019-12-17 15:47:21
问题 I have a query ordered by column : select * from mytable order by column asc — sort table column type is varchar, so the output is: 1 10 100 11 12 13 How should I sort if I want them to sort by numeric value so the output is: 1 10 11 12 13 100 回答1: Use: order by cast(column as unsigned) asc 回答2: You can use this if you want to treat column as INT only: SELECT * FROM mytable ORDER BY column+0; 1 10 11 12 13 100 Or this if you want to treat column as both INT and VARCHAR SELECT * FROM mytable

Custom Order in Oracle SQL

旧时模样 提交于 2019-12-17 15:15:19
问题 I need to order transaction based on the currency. However I need to implement a custom order by, which makes the USD to always comes on the top, and the rest should be ordered asc. for example : BHT USD MYR JYP should be sorted like : USD BHT JPY MYR Is there a simple way to handle this? 回答1: Don't know if this qualifies as simple: order by case when currency = 'USD' then 1 when currency = 'BHT' then 2 when currency = 'JPY' then 3 when currency = 'MYR' then 4 else 5 end or a bit more compact

Dynamic order direction

泄露秘密 提交于 2019-12-17 10:57:37
问题 I writing a SP that accepts as parameters column to sort and direction. I don't want to use dynamic SQL. The problem is with setting the direction parameter. This is the partial code: SET @OrderByColumn = 'AddedDate' SET @OrderDirection = 1; … ORDER BY CASE WHEN @OrderByColumn = 'AddedDate' THEN CONVERT(varchar(50), AddedDate) WHEN @OrderByColumn = 'Visible' THEN CONVERT(varchar(2), Visible) WHEN @OrderByColumn = 'AddedBy' THEN AddedBy WHEN @OrderByColumn = 'Title' THEN Title END 回答1: You

SQLite Order By Date1530019888000

 ̄綄美尐妖づ 提交于 2019-12-17 08:54:39
问题 Every record in my SQLite database contains a field which contains a Date stored as a string in the format 'yyyy-MM-dd HH:mm:ss' . Is it possible to query the database to get the record which contains the most recent date please? 回答1: you can do it like this SELECT * FROM Table ORDER BY date(dateColumn) DESC Limit 1 回答2: For me I had my query this way to solve my problem select * from Table order by datetime(datetimeColumn) DESC LIMIT 1 Since I was storing it as datetime not date column 回答3:

C# list.Orderby descending

怎甘沉沦 提交于 2019-12-17 08:16:13
问题 I would like to receive a list sorted by 'Product.Name' in descending order . Similar to the function below which sorts the list in ascending order, just in reverse, is this possible? var newList = list.OrderBy(x => x.Product.Name).ToList(); 回答1: Sure: var newList = list.OrderByDescending(x => x.Product.Name).ToList(); Doc: OrderByDescending(IEnumerable, Func). In response to your comment: var newList = list.OrderByDescending(x => x.Product.Name) .ThenBy(x => x.Product.Price) .ToList(); 回答2:

Linq To Sql - Dynamic OrderBy - Case When

前提是你 提交于 2019-12-17 07:52:12
问题 I'm using Linq to sql and Linq Dynamic OrderBy. I know linq dynamic can do simple sorting like - orderby("column_name"). But does it support something more complex like queries with "CASE WHEN" in them ? string orderbyQuery = "(CASE WHEN (username == 100) THEN 1 ELSE 0 END) DESC)"; here is my query : var u = from u in db.users orderby(orderbyQuery) select u; the above example doesn't work! , any idea if its possible? any other way to do it? thanks 回答1: var u = from u in db.users orderby u

SQL ORDER BY date problem

瘦欲@ 提交于 2019-12-17 07:26:26
问题 Can you please help me in solving this problem. I am trying to order the results of an SQL query by date, but I'm not getting the results I need. The query I'm using is: SELECT date FROM tbemp ORDER BY date ASC Results are: 01/02/2009 03/01/2009 04/06/2009 05/03/2009 06/12/2008 07/02/2009 Results should be: 06/12/2008 03/01/2009 01/02/2009 07/02/2009 I need to select the date in the format above. Your help is much appreciated. 回答1: It seems that your date column is not of type datetime but

Hibernate order by with nulls last

旧街凉风 提交于 2019-12-17 07:21:21
问题 Hibernate used with PostgreSQL DB while ordering desc by a column puts null values higher than not null ones. SQL99 standard offers keyword "NULLS LAST" to declare that null values should be put lower than not nulls. Can "NULLS LAST" behaviour be achieved using Hibernate's Criteria API? 回答1: Given that HHH-465 is not fixed and is not going to get fixed in a near future for the reasons given by Steve Ebersole, your best option would be to use the CustomNullsFirstInterceptor attached to the