sql-order-by

SQL: Sort by priority, but put 0 last

我的未来我决定 提交于 2019-12-09 00:03:17
问题 I have a (int) column called "priority". When I select my items I want the highest priority (lowest number) to be first, and the lowest priority (highest number) to be the last. However, the items without a priority (currently priority 0) should be listed by some other column after the ones with a priority. In other words. If I have these priorities: 1 2 0 0 5 0 8 9 How do I sort them like this: 1 2 5 8 9 0 0 0 I guess I could use Int.max instead of 0, but 0 makes up such a nice default value

Mysql order items by the newest of 2 dates

柔情痞子 提交于 2019-12-08 23:19:57
问题 I have a table with 2 dates - the ADDDATE and UPDATEDATE. When an item is added, the ADDDATE is the current date and the UPDATEDATE is 0000-00-00. I need to ORDER BY the SQL command and get the best date of those 2. For example if ADD=12/1/2010 and UPDATE=30/6/2010, the UPDATE DATE is best (newest). Any advice? 回答1: Use GREATEST to find the newest date: ORDER BY GREATEST(UPDATEDATE, ADDDATE) 回答2: ORDER BY IF(UPDATEDATE > ADDDATE, UPDATEDATE, ADDDATE) 回答3: If you want to make your database

MySQL WHERE IN Query - ORDER BY Match

人走茶凉 提交于 2019-12-08 19:32:15
问题 I'm trying to rephrase my question, cause my last one wasn't clear to everyone. This is my Test Table +----------+---------+-------+ | rel_id | genre | movie | +----------+---------+-------+ | 1 | 1 | 3 | | 2 | 8 | 3 | | 3 | 3 | 3 | | 4 | 2 | 5 | | 5 | 8 | 5 | | 6 | 3 | 5 | | 7 | 1 | 8 | | 8 | 8 | 8 | | 9 | 3 | 8 | | 10 | 5 | 9 | | 11 | 7 | 9 | | 12 | 9 | 9 | | 13 | 4 | 9 | | 14 | 12 | 9 | | 15 | 1 | 10 | | 16 | 8 | 10 | | 17 | 3 | 10 | | 18 | 5 | 10 | | 19 | 1 | 11 | | 20 | 2 | 11 | | 21 | 8

is it possible to use ORDER BY column not in the GROUP BY?

拟墨画扇 提交于 2019-12-08 17:45:13
问题 like the title said, here is my code: SELECT material, SUM([Amount]) AS [Amount], RIGHT(CONVERT(varchar(50), [date_in], 106), 8) FROM [rec_stats] GROUP BY material, RIGHT(CONVERT(varchar(50), [date_in], 106), 8) ORDER BY material,date_in the code wont run because of the date_in, is there anyway to get around this? 回答1: Apply another aggregate, so how about; order by min([date_in]) 回答2: Order by the same expression you're grouping by. It is better to group and order on the DATE representation

Rails: Order by sum of two columns

最后都变了- 提交于 2019-12-08 17:38:25
问题 So, I have a Photo model which can be downloaded at full_size and presentation_size . When a user downloads a photo I track this on the photo's full_downloads and presentation_downloads attribute. That's all good. Sometimes I want to know how many total downloads there have been. I have a simple method, total_downloads which looks like so: def total_downloads self.full_downloads + self.presentation_downloads end My question is: I would like to be able to order photos by all three of these

GROUP BY after ORDER BY

与世无争的帅哥 提交于 2019-12-08 17:06:27
问题 I need to do GROUP BY after ORDER BY . I don't understand why MySQL doesn't support that. This is my code: SELECT `pages`.`id`, `contents`.`id_language`, [...] [...] ORDER BY FIND_IN_SET(`languages`.`id`, '3') DESC [the GROUP BY] The results will be something like this: id | id_language | ... 1 3 1 1 2 3 2 5 2 1 I need to group by ID, I need only the first result and I need to save in a view. I can't use a SUBQUERY because of that. The result need to be: id | id_language | ... 1 3 2 3 Note:

Return order of MySQL SHOW COLUMNS

人走茶凉 提交于 2019-12-08 16:38:16
问题 I need to find the columns in a specific table, which is no problem: SHOW COLUMNS FROM tablename LIKE '%ColumnPrefix%'; But I need to know what order they will be returned, preferably by choosing to order the results ascending alphabetically. I have had no luck with using ORDER BY . Any ideas? 回答1: You can query the table INFORMATION_SCHEMA.COLUMNS to get the information that SHOW COLUMNS gives you, plus it allows you to use ORDER BY or any other SQL syntax you might want to use: SELECT

How to sort within a sql view

∥☆過路亽.° 提交于 2019-12-08 16:01:35
问题 I've created a sql view and I need to sort the results of select by using the ORDER BY on 4 fields, but I'm getting message that ORDER BY cannot be used in views unless I use TOP. Can someone explain why TOP is needed, and does someone have a workaround for sorting in a sql view? Thanks. 回答1: you don't need to sort a view. a view is like a table so you sort it when you select from it: select * from yourView order by yourColumns 回答2: There is no guarantee the output of the view will be ordered

Order by does not work with Concat() in LINQ

六月ゝ 毕业季﹏ 提交于 2019-12-08 14:46:01
问题 Using VB.net and the following LINQ statement. I suspect the "Order by" does not work with Concat() . I want to list the current item the user has and then list more available items in asending order. So first i select the current item from the db and then select the next available items in order. LINQ is ignoring the order by statement and sorting by the PK (which is itemID) I examined the list immediately after executing the statement. When I break up the statement and do them separately

What's the syntax for ORDERBY in VB.NET's LINQ?

风格不统一 提交于 2019-12-08 14:42:06
问题 I'm new to Linq, what's the syntax for order by in VB? Dim cxt As New datContext Dim qry = (From lst In cxt.zipcodes _ Select lst.state).Distinct qry = qry.OrderBy() my simple SQL statement will be like this: Select distinct state from zipcodes order by State 回答1: qry = qry.OrderBy(Function(obj) obj.PropertyToSortBy) 回答2: Alternative syntax for your query (cleaner IMO): Dim qry = From lst In cxt.zipcodes _ Select lst.state Distinct _ Order By state 回答3: Dim cxt As New datContext Dim qry =