sql-order-by

Sql Server query varchar data sort like int

眉间皱痕 提交于 2019-12-03 11:46:49
问题 I have one table like CREATE TABLE table_name ( P_Id int, amount varchar(50) ) Data Like Id amount ---------- 1 2340 2 4568 3 10000 Now I want to sort table by amount but one problem is amount is varchar so it sort table like this Id amount ---------- 3 10000 1 2340 2 4568 but i want result like this Id amount ---------- 3 10000 2 4568 1 2340 what should i do ? 回答1: Cast amount column into Numeric in ORDER BY clause while selecting: SELECT * FROM MyTable ORDER BY CAST(amount AS Numeric(10,0))

Linq OrderBy breaks with navigation property being null

僤鯓⒐⒋嵵緔 提交于 2019-12-03 11:00:12
问题 Working with four tables. Users -> has basic user info including a userid and a departmentid (int) Groups -> basic group info including a groupid GroupsMembers -> table that has the relationship between a group and it's members, many to many relationship, so groupid and userid are the columns Departments -> basic department info including deptid I have a fk from the departmentid in the users table to the deparmtnet id in the departments table. FK from groups groupid to groupsmembers groupid

How to use OFFSET and Fetch without Order by in SQL Server

百般思念 提交于 2019-12-03 10:50:32
I want use OFFSET and Fetch in my SQL server 2012 query.But without any order by.I can not use order by.Because my sort order will be lost. How can I use OFFSET and Fetch without order by and row number and where in my query? My 2 select tables have same structure. INSERT INTO @TempTable [some columns] select [some columns] from table1 order by col1 INSERT INTO @TempTable [same columns] select [some columns] from table2 order by col2 select * from @TempTable OFFSET 20 ROWS FETCH NEXT 50 ROWS ONLY This query has syntax error at OFFSET keyword. By adding an identity column to the temp table

How can I sort by a table column in varying cases (Oracle)

夙愿已清 提交于 2019-12-03 09:41:55
How can I sort a table with a column of varchar2 with characters in varying cases ( UPPER and lower )? For example, when I do an order by of the Name column, I get the following results: ANNIE BOB Daniel annie bob What I want is something like this: ANNIE annie BOB bob Daniel Use lower(field) , e.g. select * from tbl order by lower(name) If you need to address special characters for non-english languages then the other answers about NLSSORT may be what you need. If you don't I would try and KISS and use lower() as it is very easy to remember and use and be read by others (maintainability).

MYSQL Select from table, get newest/last 10 rows in table

一曲冷凌霜 提交于 2019-12-03 09:24:54
问题 What's the best, and easiest way to do this? My query currently is: SELECT * FROM chat WHERE (userID = $session AND toID = $friendID) OR (userID = $friendID AND toID = $session) ORDER BY id LIMIT 10 This shows the first 10 rows though, not the last 10. EDIT: I Want the last 10 rows (Which yes, DESC does this) However I want them to be returned in ASCENDING order. 回答1: to reverse the order (therefore get last 10 instead of first 10), use DESC instead of ASC EDIT Based on your comment: SELECT *

Basic Rails 3 question: How to sort products?

这一生的挚爱 提交于 2019-12-03 08:59:58
I have the following models: Product: name, shop_id (foreign key), brand_id (foreign key), price Shop: name Brand: name The associations are: Product: belongs_to :shop belongs_to :brand Shop: has_many :products has_many :brands, :through => :products Brand: has_many :products has_many :shops, :through => :products In ProductsController#list I would like to get a list of all products sorted by shop name and then by brand name. I tried to do: @products = Product.order("products.shop.name ASC, products.brand.name ASC") But it doesn't work (I guess because products.shop.name does not exist at the

MySQL multiple column asc order

女生的网名这么多〃 提交于 2019-12-03 07:26:39
问题 I am trying to run this query in ascending order: SELECT title,project_index FROM projectdetail WHERE project_index BETWEEN 1 AND 6 ORDER BY title, project_index ASC; I need two columns in ascending order, but the above query returns results with only one column in ASC order. 回答1: Ascending order is the default for most (if not all) DBMS's so your statement is kind of weird in that respect but nevertheless, you can specify an order for each individual column by adding the specifier ASC or

Laravel MySQL orderBy count

时间秒杀一切 提交于 2019-12-03 07:23:37
I'm using Laravel and MySQL, and I have a table post that represents post where users can comment on it, now I wanna order posts by the number of comments of each post in ascending/descending order, how do I do this in Laravel? I don't want to add a field in post table to keep track of the number of comments of each post, because updating that field manually each time a comment or a comment's comment is added/deleted drives me crazy... This is how I create my posts table and comments table: Schema::create('posts', function($table) { $table->increments('id'); $table->string('title', 100)-

TSQL ORDER BY with nulls first or last (at bottom or top)

安稳与你 提交于 2019-12-03 06:46:11
问题 I have a date column which has some NULL . I want to order by the date column ASC, but I need the NULL s to be at the bottom. How to do it on TSQL ? 回答1: In standard SQL you can specify where to put nulls: order by col asc nulls first order by col asc nulls last order by col desc nulls first order by col desc nulls last but T-SQL doesn't comply with the standard here. The order of NULLs depends on whether you sort ascending or descending in T-SQL: order by col asc -- implies nulls first order

MySQL order by primary key

对着背影说爱祢 提交于 2019-12-03 06:45:25
Some SQL servers allow for a generic statement such as ORDER BY PRIMARY KEY . I don't believe this works for MySQL, is there any such workaround that would allow for automated selects across multiple tables or does it require a lookup query to determine the primary key? The workaround I have been working on involves calling a SHOW COLUMNS FROM before running the query. Is there a more efficient way of doing this? Can MySQL determine the primary key of a table during the select process? Update: There is no official way of doing this in MySQL or SQL in general as Gordon pointed out. SAP has