sql-order-by

Using distinct on a column and doing order by on another column gives an error

纵饮孤独 提交于 2019-12-04 10:31:30
问题 I have a table: abc_test with columns n_num, k_str. This query doesnt work: select distinct(n_num) from abc_test order by(k_str) But this one works: select n_num from abc_test order by(k_str) How do DISTINCT and ORDER BY keywords work internally that output of both the queries is changed? 回答1: As far as i understood from your question . distinct :- means select a distinct(all selected values should be unique). order By :- simply means to order the selected rows as per your requirement . The

MySQL query to show records with current date on top and others as per descending order

佐手、 提交于 2019-12-04 10:24:49
I am using the following query in my database, SELECT b.sales_id,b.category_id,b.sale_starts,b.sale_ends FROM tbl_sales b WHERE b.active=1 UNION SELECT b.sales_id,b.category_id,b.sale_starts,b.sale_ends FROM tbl_sales b INNER JOIN tb_category c ON b.category_id=c.cat_id WHERE c.cat_keyword LIKE 'a' ORDER BY sale_ends DESC and getting the result as follows, sales_id | category_id |sale_starts | sale_ends ----------|---------------------|------------|-------------- 1 | 10 | 2012-03-31 | 2012-04-30 2 | 11 | 2012-03-22 | 2012-04-27 3 | 25 | 2012-03-31 | 2012-04-25 4 | 12 | 2012-04-05 | 2012-04-11

Order by clause execution in SQL

自古美人都是妖i 提交于 2019-12-04 10:11:52
This question isn't about order of executions. It's about just the ORDER BY. In standard execution is: FROM WHERE GROUP BY HAVING SELECT ORDER BY TOP EDIT: This question has been more or less the issue of " Does SQL Server apply short circuit evaluation when executing ORDER BY expressions? " The answer is SOMETIMES! I just haven't found a reasonable reason as to why. See Edit #4. Now suppose I have a statement like this: DECLARE @dt18YearsAgo AS DATETIME = DATEADD(YEAR,-18,GETDATE()); SELECT Customers.Name FROM Customers WHERE Customers.DateOfBirth > @dt18YearsAgo ORDER BY Contacts.LastName

max(), group by and order by

大憨熊 提交于 2019-12-04 08:39:00
I have following SQL statement. SELECT t.client_id,max(t.points) AS "max" FROM sessions GROUP BY t.client_id; It simply lists client id's with maximum amount of points they've achieved. Now I want to sort the results by max(t.points). Normally I would use ORDER BY, but I have no idea how to use it with groups. I know using value from SELECT list is prohibited in following clauses, so adding ORDER BY max at the end of query won't work. How can I sort those results after grouping, then? Best regards SELECT t.client_id, max(t.points) AS "max" FROM sessions t GROUP BY t.client_id order by max(t

“Order by” result of “group by” count?

爱⌒轻易说出口 提交于 2019-12-04 07:29:29
问题 This query Message.where("message_type = ?", "incoming").group("sender_number").count will return me an hash. OrderedHash {"1234"=>21, "2345"=>11, "3456"=>63, "4568"=>100} Now I want to order by count of each group. How can I do that within the query. 回答1: The easiest way to do this is to just add an order clause to the original query. If you give the count method a specific field, it will generate an output column with the name count_{column}, which can be used in the sql generated by adding

Sorting Nulls last

狂风中的少年 提交于 2019-12-04 07:16:46
Want to sort Nulls and Blanks last and have read all the solutions that use either the Coalesce function or the If in the order by column. MY problems is non of these work for me because the column I am sorting on is specified dynamically by the PHP script that creates the query and some of my columns are in two tables in my join. $sortcol="boat"; $sql= "SELECT fleet,b.boat as boat,owner FROM boats as b LEFT JOIN owners as o ON b.boat=o.boat ORDER BY $sortcol"; This works great, I can change the variable $sortcol and my output listing works great except the nulls and blanks are at the top.

Order by last 2 characters string

拜拜、爱过 提交于 2019-12-04 06:05:44
This is the result of my query, but it doesn't order correctly. I want to order by the last 2 characters. The result should be: Fa0/10 below Fa0/9 . Fa0/1 Fa0/10 Fa0/11 Fa0/12 Fa0/2 Fa0/3 Fa0/4 Fa0/5 Fa0/6 Fa0/7 Fa0/8 Fa0/9 Gi0/1 Gi0/2 Null0 Vlan1 My query: SELECT inft.port FROM interfaces AS intf ORDER BY RIGHT(intf.port + 0, 2) Second: sqlfiddle Try this: SELECT port FROM interfaces ORDER BY SUBSTRING_INDEX(port, '/', 1), CAST(SUBSTRING_INDEX(port, '/', -1) AS SIGNED) Check the SQL FIDDLE DEMO OUTPUT | PORT | |--------| | Fa0/1 | | Fa0/2 | | Fa0/3 | | Fa0/4 | | Fa0/5 | | Fa0/6 | | Fa0/7 | |

mysql: how to save ORDER BY after LEFT JOIN without reorder?

折月煮酒 提交于 2019-12-04 06:04:24
问题 I've two table: 1) profiles +----+---------+ | id | name | +----+---------+ | 1 | WILLIAM | | 2 | JOHN | | 3 | ROBERT | | 4 | MICHAEL | | 5 | JAMES | | 6 | DAVID | | 7 | RICHARD | | 8 | CHARLES | | 9 | JOSEPH | | 10 | THOMAS | +----+---------+ 2) request_for_friendship +----+---------+-------+ | id | from_id | to_id | +----+---------+-------+ | 1 | 1 | 2 | | 2 | 1 | 3 | | 3 | 1 | 8 | | 5 | 4 | 1 | | 6 | 9 | 1 | +----+---------+-------+ I need to get all profiles with some sorting and join it

Give priority to ORDER BY over a GROUP BY in MySQL without subquery

梦想的初衷 提交于 2019-12-04 05:42:05
I have the following query which does what I want, but I suspect it is possible to do this without a subquery: SELECT * FROM (SELECT * FROM 'versions' ORDER BY 'ID' DESC) AS X GROUP BY 'program' What I need is to group by program, but returning the results for the objects in versions with the highest value of "ID". In my past experience, a query like this should work in MySQL, but for some reason, it's not: SELECT * FROM 'versions' GROUP BY 'program' ORDER BY MAX('ID') DESC What I want to do is have MySQL do the ORDER BY first and then the GROUP BY, but it insists on doing the GROUP BY first

Using Order By in codeigniter

房东的猫 提交于 2019-12-04 05:27:38
问题 This is my normal mysql query: $sql = "SELECT * FROM item order by "; if(my_condition) { $sql. = "FIELD(`category`, 'Auto & Accessories', 'Couch', 'Bed', 'Desk & Office', 'Bike & Scooter', 'Tools', 'Leisure', 'Iron & Wood', 'Cabinet', 'Kitchen & Accessories', 'Refrigerator & Appliances', 'Toys & Games', 'Chair', 'Table', 'Garden & Terrace', 'TV, HIFI & Computers', 'General Item')"; } else { $sql .= "category asc"; } I need it in CI in active record. I tried in following way: if(my_condition)