sql-order-by

linq to entities orderby strange issue

一世执手 提交于 2020-01-14 05:30:12
问题 maybe foolish question, first times with linq to entities (well, linq in general). table with id(int), value(decimal), name(string) for each record i need id list<string> id value name THE FOLLOWING WORKS FINE int pageSize=10 int pageIndex=2 var data = (from c in db.Customers orderby c.ID select new { c.ID, c.Value, c.Name } ).Skip(pageSize * pageIndex).Take(pageSize).ToArray(); but doesn'organize the data in the way i need them. However the results are like: 1 100 name A 2 300 name B 3 200

MySQL Custom Order

本小妞迷上赌 提交于 2020-01-13 19:09:18
问题 I have a table that I select data from with a column called parent that's of unsigned integer type. It has numbers from 0 to 12. I want to select * from table order by parent asc, but with one exception: place the 0 at the end of the select so it would be like 1,2,3,4,5,6,7,8,9,0 . Is this possible with a single select in MySQL please? 回答1: I would do something like this: select * from your_table order by (parent != 0) desc, parent asc; 回答2: select * from table order by case when parent is 0

MySQL Custom Order

丶灬走出姿态 提交于 2020-01-13 19:07:19
问题 I have a table that I select data from with a column called parent that's of unsigned integer type. It has numbers from 0 to 12. I want to select * from table order by parent asc, but with one exception: place the 0 at the end of the select so it would be like 1,2,3,4,5,6,7,8,9,0 . Is this possible with a single select in MySQL please? 回答1: I would do something like this: select * from your_table order by (parent != 0) desc, parent asc; 回答2: select * from table order by case when parent is 0

SQL query ordering in custom order

醉酒当歌 提交于 2020-01-13 18:39:07
问题 I have a custom ordering need like this: normal ordering | custom ordering 1 | 7 2 | 6 3 | 5 4 | 4 5 | 3 6 | 2 7 | 8 . | . . | . . | . . | . . | N N | 1 I have thought about using UNION to combine 3 different select queries with the help of ORDER BY and LIMIT . However, I can not do that because UNION have to be used before ORDER BY and LIMIT . How can I make a selection (or selections) to achieve the custom ordering above? Another workaround might help is just make the 1st record returned in

Doctrine2 Order By before Group By

流过昼夜 提交于 2020-01-13 06:15:13
问题 I am having issues implementing a sub-select solution for ORDERING a resulting dataset before the GROUP BY reduces it. Normally, in SQL you would do a sub-select: SELECT * FROM ( SELECT * FROM a_table order by a_table.timestamp desc ) as table_tmp group by userId However, I am having difficulty implementing this in DQL. Can anyone point me in the right direction please? My query is more complex than this and I assume I JOIN other tables through 'table_tmp' and in the outer SELECT. Thanks. 回答1

mysql order varchar field as integer

两盒软妹~` 提交于 2020-01-11 19:57:49
问题 I have a varchar field in my table and I want to sort it. But I need to handle this field as integer. Meaning if sort as text the order is "19,2,20" but I want to get the right order "2,19,20". Can anyone help me? 回答1: I somehow didn't manage to run the query with CAST . I was always getting Error Code: 1064 near "DECIMAL" (or other numeric type that I chose). So, I found another way to sort varchar as numbers: SELECT * FROM mytable ORDER BY ABS(mycol) A bit simpler and works in my case. 回答2:

MySQL not Sorting Data Correctly

你离开我真会死。 提交于 2020-01-11 12:05:25
问题 I have been using MySQL for a long time and I have never run across this issue. I have a table that stores the scores for an application. For some reason, when I sort by score ASC , the highest score is shown first with the lowest score being last. Please see the screenshot below: Here is my query: SELECT category, subject, max(score) as score FROM scores WHERE customer_id = 1086 AND category = 'Business' GROUP BY subject ORDER BY score ASC Any thoughts on why this is happening? 回答1: Change

MySQL not Sorting Data Correctly

ぐ巨炮叔叔 提交于 2020-01-11 12:05:07
问题 I have been using MySQL for a long time and I have never run across this issue. I have a table that stores the scores for an application. For some reason, when I sort by score ASC , the highest score is shown first with the lowest score being last. Please see the screenshot below: Here is my query: SELECT category, subject, max(score) as score FROM scores WHERE customer_id = 1086 AND category = 'Business' GROUP BY subject ORDER BY score ASC Any thoughts on why this is happening? 回答1: Change

ORDER BY suddenly conflicting with VARCHAR concatenation in TSQL

心不动则不痛 提交于 2020-01-11 11:45:46
问题 I have code in Sql Server 2008 which concatenates some strings from a query into a variable using the tried-and-true SELECT @VAR = @VAR + FIELD FROM TABLE ORDER BY OTHERFIELD syntax. This is my exact SQL: SELECT @SQL = @SQL + ISNULL(FORMULA, CASE WHEN USEMAP = 1 THEN 'dbo.getFieldTranslation('+CONVERT(VARCHAR,ROWID)+', [' + ISNULL(ENCOMPASSFIELD,'') + '])' ELSE '[' + ISNULL(ENCOMPASSFIELD,'') + ']' END ) + ' AS "' + FILECOLNAME + '",' + @CRLF FROM dbo.EXPORTMAP_EX WHERE WAREHOUSEID = @WHSID

ORDER BY suddenly conflicting with VARCHAR concatenation in TSQL

China☆狼群 提交于 2020-01-11 11:45:26
问题 I have code in Sql Server 2008 which concatenates some strings from a query into a variable using the tried-and-true SELECT @VAR = @VAR + FIELD FROM TABLE ORDER BY OTHERFIELD syntax. This is my exact SQL: SELECT @SQL = @SQL + ISNULL(FORMULA, CASE WHEN USEMAP = 1 THEN 'dbo.getFieldTranslation('+CONVERT(VARCHAR,ROWID)+', [' + ISNULL(ENCOMPASSFIELD,'') + '])' ELSE '[' + ISNULL(ENCOMPASSFIELD,'') + ']' END ) + ' AS "' + FILECOLNAME + '",' + @CRLF FROM dbo.EXPORTMAP_EX WHERE WAREHOUSEID = @WHSID