sql-order-by

ORDER BY alphabet first then follow by number

只谈情不闲聊 提交于 2019-11-26 22:23:10
问题 I looking for some tweak in mysql ordering , I normally select record from table and then order the record by Name(varchar) ASC but the number is always come first here some example of my question ( note. mysql sort the record with 0-9 first ) SELECT name FROM list ORDER BY name ASC record returned: 1 star 2 star 9 slice Ape Age Beg Bell Fish Zoo What i want is the alphabet order come first then follow by number Desired output Ape Age Beg Bell Fish Zoo 1 star 2 star 9 slice 回答1: Use the

Order by COUNT per value

爷,独闯天下 提交于 2019-11-26 22:18:50
I have a table which stores IDs and the city where the store is located. I want to list all the stores starting with the stores that are in the city where there are the most stores. TABLE ID CITY 1 NYC 2 BOS 3 BOS 4 NYC 5 NYC The output I want is the following since I have the most stores in NYC, I want all the NYC location to be listed first. 1 NYC 4 NYC 5 NYC 2 BOS 3 BOS SELECT count(City), City FROM table GROUP BY City ORDER BY count(City); OR SELECT count(City) as count, City FROM table GROUP BY City ORDER BY count; Ahh, sorry, I was misinterpreting your question. I believe Peter Langs

Linq order by, group by and order by each group?

房东的猫 提交于 2019-11-26 21:57:59
I have an object that looks something like this: public class Student { public string Name { get; set; } public int Grade { get; set; } } I would like to create the following query: group grades by student name, order each student group by grades, and order groups by max grade in each group. So it will look like this: A 100 A 80 B 80 B 50 B 40 C 70 C 30 I created the following query: StudentsGrades.GroupBy(student => student.Name) .OrderBy(studentGradesGroup => studentGradesGroup.Max(student => student.Grade)); But that returns IEnumerable IGrouping , and I have no way to sort the list inside,

MySQL ORDER BY IN()

旧街凉风 提交于 2019-11-26 21:54:59
I have a PHP array with numbers of ID's in it. These numbers are already ordered. Now i would like to get my result via the IN() method, to get all of the ID's. However, these ID's should be ordered like in the IN method. For example: IN(4,7,3,8,9) Should give a result like: 4 - Article 4 7 - Article 7 3 - Article 3 8 - Article 8 9 - Article 9 Any suggestions? Maybe there is a function to do this? Thanks! Alex Martelli I think you may be looking for function FIELD -- while normally thought of as a string function, it works fine for numbers, too! ORDER BY FIELD(field_name, 3,2,5,7,8,1) You

LINQ OrderBy not ordering .. changing nothing .. why?

戏子无情 提交于 2019-11-26 21:46:13
问题 Any idea why the LINQ OrderBy is not working in following code, (have no errors but method does not sort ...) First my own type public class IQLinksView { public int id { get; set; } public int catid { get; set; } public int? viewed {get;set;} public string name {get;set;} public string desc {get;set;} public string url {get;set;} public string pic {get;set;} public string cat {get;set;} } then query : IQueryable<IQLinksView> newView = from links in this.emContext.tbl_otherlinks select new

Is it possible to use bind_param for ORDER BY? [duplicate]

狂风中的少年 提交于 2019-11-26 21:38:20
问题 This question already has an answer here: Can I parameterize the table name in a prepared statement? 2 answers In my mind I have a query that goes something like this: $sort = isset($sort) ? sanitize($_sort) : 'id'; if ($result = $link->prepare(" SELECT id, price FROM items ORDER BY ? ")) { $result->bind_param("s", $sort); $result->execute(); etc... } When I run this code block without setting the sort variable it runs without an error relating to the use of the ? in the ORDER BY clause and a

LINQ to SQL does not generate ORDER BY when DISTINCT is used?

≡放荡痞女 提交于 2019-11-26 20:57:06
问题 The following basic LINQ to SQL statement does not result in the orderby working. As you can see in the T-SQL there is no orderby. Do you know why? LINQ to SQL: var results = (from stats in db.t_harvest_statistics orderby stats.unit_number select stats.unit_number).Distinct().ToList(); Above Results in following TSQL SELECT [Distinct1].[unit_number] AS [unit_number] FROM ( SELECT DISTINCT [Extent1].[unit_number] AS [unit_number] FROM [dbo].[t_harvest_statistics] AS [Extent1] ) AS [Distinct1]

Curious issue with Oracle UNION and ORDER BY

别说谁变了你拦得住时间么 提交于 2019-11-26 20:52:36
问题 The following query is perfectly valid in pretty much every database (give or take a dual dummy table), including Oracle: select 'A' as x from dual union all select 'B' from dual order by x asc Returning: | X | |---| | A | | B | Now this query is still quite standard SQL, but doesn't work on Oracle select 'A' as x from dual union all select 'B' from dual union all select 'C' from dual order by x asc I'm getting ORA-00904: "X": invalid identifier This, however, works: select 'A' as x from dual

SQL Server UNION - What is the default ORDER BY Behaviour

老子叫甜甜 提交于 2019-11-26 20:52:22
If I have a few UNION Statements as a contrived example: SELECT * FROM xxx WHERE z = 1 UNION SELECT * FROM xxx WHERE z = 2 UNION SELECT * FROM xxx WHERE z = 3 What is the default order by behaviour? The test data I'm seeing essentially does not return the data in the order that is specified above. I.e. the data is ordered, but I wanted to know what are the rules of precedence on this. Another thing is that in this case xxx is a View. The view joins 3 different tables together to return the results I want. There is no default order. Without an Order By clause the order returned is undefined.

How to ORDER BY a SUM() in MySQL?

北慕城南 提交于 2019-11-26 20:41:48
I have a table: "ID name c_counts f_counts " and I want to order all the record by sum(c_counts+f_counts) but this doesn't work: SELECT * FROM table ORDER BY sum(c_counts+f_counts) LIMIT 20; Don'y forget that if you are mixing grouped (ie. SUM) fields and non-grouped fields, you need to GROUP BY one of the non-grouped fields. Try this: SELECT SUM(something) AS fieldname FROM tablename ORDER BY fieldname OR this: SELECT Field1, SUM(something) AS Field2 FROM tablename GROUP BY Field1 ORDER BY Field2 And you can always do a derived query like this: SELECT f1, f2 FROM ( SELECT SUM(x+y) as f1, foo