sql-order-by

MYSQL order by time am/pm

会有一股神秘感。 提交于 2019-12-18 17:32:34
问题 I have a row in a table which holds the time of day in am/pm format e.g. timeField --------- 9.00am 10.00pm 7.00am etc... Is there a way in mysql to order these values? 回答1: You can do this by using STR_TO_DATE function in MySQL: SELECT STR_TO_DATE('10.00pm','%h.%i%p'); try this: SELECT * FROM table_name ORDER BY STR_TO_DATE(timeField,'%h.%i%p'); Example: SQLFiddle 回答2: In order to order it you need to format the date field fist. try this SELECT COl1, COl2, DATE_FORMAT(yourdate, '%k:%i:%s')

Linq Query with SUM and ORDER BY

狂风中的少年 提交于 2019-12-18 14:56:39
问题 I have a (C#) class called Hit with an ItemID (int) and a Score (int) property. I skip the rest of the details to keep it short. Now in my code, I have a huge List on which I need to do the following select (into a new List): I need to get the sum of all Hit.Score's for each individual Hit.ItemID, ordered by Score. So if I have the following items in the original list ItemID=3, Score=5 ItemID=1, Score=5 ItemID=2, Score=5 ItemID=3, Score=1 ItemID=1, Score=8 ItemID=2, Score=10 the resulting

SQL Show most recent record in GROUP BY?

烈酒焚心 提交于 2019-12-18 14:15:19
问题 I have a table that looks like this: id | SubjectCode | Grade | DateApproved | StudentId 1 SUB123 1.25 1/4/2012 2012-12345 2 SUB123 2.00 1/5/2012 2012-12345 3 SUB123 3.00 1/5/2012 2012-98765 I'm trying to GROUP BY SubjectCode but i'd like it to display the most recent DateApproved so it will look like: id | SubjectCode | Grade | DateApproved | StudentId 2 SUB123 2.00 1/5/2012 2012-12345 3 SUB123 3.00 1/5/2012 2012-98765 I'm a little bit lost on how to do it? EDIT: Ok guys now im on my real PC

SQL order by a column from another table

断了今生、忘了曾经 提交于 2019-12-18 14:14:07
问题 I have 3 tables: people, groups and memberships. Memberships is a join table between people and groups, and have 3 columns: personId, groupId and description (text). I want to select entries from the memberships table depending on a groupId but sorting the result by the names of people associated to the found memberships (name is a column of people table) SELECT * FROM "memberships" WHERE ("memberships".groupId = 32) ORDER BY (?????) Is it possible to achieve this in one single query? 回答1:

ORDER BY AND CASE IN SQL SERVER

若如初见. 提交于 2019-12-18 13:37:23
问题 I need to have an order by functionality inside a stored procedure. A value is posted to a webservice and based on that value I have to order the results in a certain way i.e. When ColName is posted order by ColName When ColName2 is posted order by ColName2 I was looking into using Case but I am getting an error: Incorrect syntax near '@version' ORDER BY CASE WHEN @OrderBy ='Seller (code)' THEN A_SNO WHEN @OrderBy ='Lot' THEN A_LOTNO WHEN @OrderBy ='Ring Type' THEN RN_NUM WHEN @OrderBy ='Aim

Postgres CASE in ORDER BY using an alias

送分小仙女□ 提交于 2019-12-18 12:16:39
问题 I have the following query which works great in Postgres 9.1: SELECT users.id, GREATEST( COALESCE(MAX(messages.created_at), '2012-07-25 16:05:41.870117'), COALESCE(MAX(phone_calls.created_at), '2012-07-25 16:05:41.870117') ) AS latest_interaction FROM users LEFT JOIN messages ON users.id = messages.user_id LEFT JOIN phone_calls ON users.id = phone_calls.user_id GROUP BY users.id ORDER BY latest_interaction DESC LIMIT 5; But what I want to do is something like this: SELECT users.id, GREATEST(

Best practice question for MySQL: order by id or date?

北战南征 提交于 2019-12-18 12:01:08
问题 This is kind of a noobish question, but it's one that I've never been given a straight answer on. Suppose I have a DB table with the following fields and values: | id | date_added | balance | +------------------------------------+ | 1 | 2009-12-01 19:43:22 | 1237.50 | | 2 | 2010-01-12 03:19:54 | 473.00 | | 3 | 2010-01-12 03:19:54 | 2131.20 | | 4 | 2010-01-20 11:27:31 | 3238.10 | | 5 | 2010-01-25 22:52:07 | 569.40 | +------------------------------------+ This is for a very basic 'accounting'

How to order 1,2,3 not 1, 10, 11, 12 in mySQL

﹥>﹥吖頭↗ 提交于 2019-12-18 11:42:02
问题 The following code outputs in order of 1, 10, 11, 12 of id. I want to make it 1,2,3,4... Could anyone tell me what I should do please. $Q = $this->db->query('SELECT P.*, C.Name AS CatName FROM products AS P LEFT JOIN categories C ON C.id = P.category_id'); Thanks in advance. 回答1: First, add an order by clause at the end: ORDER BY category_id If category_id is a string, then you must treat it like an integer. There are a few ways to do this. I usually add a zero. You can also cast it. ORDER BY

MySql: ORDER BY parent and child

会有一股神秘感。 提交于 2019-12-18 10:37:26
问题 I have a table like: +------+---------+- | id | parent | +------+---------+ | 2043 | NULL | | 2044 | 2043 | | 2045 | 2043 | | 2049 | 2043 | | 2047 | NULL | | 2048 | 2047 | | 2043 | 2047 | +------+---------+ which shows a simple, 2-level "parent-child"-corelation. How can I ORDER BY an SELECT-statement to get the order like in the list above, which means: 1st parent, childs of 1st parent, 2nd parent, childs of 2nd parent and so on (if I have that, I can add the ORDER BYs for the children... I

How do I order a Group result, in Linq?

泄露秘密 提交于 2019-12-18 10:33:31
问题 I have the following linq query, which works fine. I'm not sure how i order the group'd result. from a in Audits join u in Users on a.UserId equals u.UserId group a by a.UserId into g select new { UserId = g.Key, Score = g.Sum(x => x.Score) } the results are currently ordered by UserId ascending. I'm after Score descending. thanks :) 回答1: Just add the orderby clause ;-) from a in Audits join u in Users on a.UserId equals u.UserId group a by a.UserId into g let score = g.Sum(x => x.Score)