sql-order-by

order by a parameter

孤街醉人 提交于 2019-11-27 06:44:00
问题 Hi i have a store procedure, where i do a select query. I want order this by an external parameter. I post an minimal example: CREATE PROCEDURE [dbo].[up_missioni_get_data] @order VarChar(100) AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here SELECT * from missioni ORDER BY ... END What can i write in order by for do that? thanks 回答1: You have 2 options, either use a CASE statement,

UPDATE with ORDER BY

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 06:40:25
问题 Need to "tie" UPDATE with ORDER BY . I'm trying to use cursors, but get the error: cursor "cursupd" doesn't specify a line, SQL state: 24000 Code: BEGIN; DECLARE cursUpd CURSOR FOR SELECT * FROM "table" WHERE "field" = 5760 AND "sequence" >= 0 AND "sequence" < 9 ORDER BY "sequence" DESC; UPDATE "table" SET "sequence" = "sequence" + 2 WHERE CURRENT OF cursUpd; CLOSE cursUpd; COMMIT; How to do it correctly? UPDATE 1 Without cursor, when I do like this: UPDATE "CableLinePoint" AS "t" SET

MySQL order posts by most recent comment OR last posted

北城以北 提交于 2019-11-27 06:27:49
问题 How can I sort posts so most recent activity is on top? # Schema not including all info, including FKs CREATE TABLE post( id int unsigned primary key auto_increment, msg text, created datetime )ENGINE=InnoDb; CREATE TABLE comment( id int unsigned primary key auto_increment, post_id int unsigned, msg text, created datetime )ENGINE=InnoDb; I want to order posts by most recent, where a new post is obviously more recent than one previously posted, but an old post that has a recent comment

Sort MySQL results alphabetically, but with numbers last

淺唱寂寞╮ 提交于 2019-11-27 06:14:31
问题 Often, sorting is done with symbols sorted to the top, like 0 or * or & . This is the default way that mysql sorts; numbers and symbols and then A-Z. However, that makes the often ugliest or most badly formatted results float to the top (e.g. a result of @#$@3423 or 8 inch or &amp ). So I'd like to do a modified form of that, letters first A-Z, and then special characters last. How would I go about creating that type of sort? Something in the ORDER BY clause? 回答1: Based on a google-cached

MySQL - How to ORDER BY RELEVANCE? INNODB Table

本小妞迷上赌 提交于 2019-11-27 06:07:46
I've got about 20,000 rows in an INNODB table called 'cards', so FULLTEXT is not an option. Please consider this table: id | name | description ---------------------------------------------------------- 1 John Smith Just some dude 2 Ted Johnson Another dude 3 Johnathan Todd This guy too 4 Susan Smith Her too 5 Sam John Bond And him 6 John Smith Same guy as num 1, another record 7 John Adams Last guy, promise So, say the user searches for 'John', I want the result set to be in the order of: 7 John Adams 6 John Smith 3 Johnathan Todd 5 Sam John Bond 2 Ted Johnson Please note that we've only

Is order by clause allowed in a subquery

旧街凉风 提交于 2019-11-27 05:59:44
问题 Is there any reason why or why not you should do an 'order by' in a subquery? 回答1: Yes: It should not be done, because it does not make sense conceptually. The subquery will be used in some outer query (otherwise it would be pointless), and that outer query will have to do ordering anyway, so there's no point ordering the subquery. This is because query results in SQL will come in no particular order, unless you use an explicit ORDER. So even if you used ORDER in the subquery, you have no

Why can't i refer to a column alias in the ORDER BY using CASE?

坚强是说给别人听的谎言 提交于 2019-11-27 05:56:07
问题 Sorry if this a duplicate, but i haven't found one. Why can't i use my column alias defined in the SELECT from the ORDER BY when i use CASE ? Consider this simple query: SELECT NewValue=CASE WHEN Value IS NULL THEN '<Null-Value>' ELSE Value END FROM dbo.TableA ORDER BY CASE WHEN NewValue='<Null-Value>' THEN 1 ELSE 0 END The result is an error: Invalid column name 'NewValue' Here's a sql-fiddle. (Replace the ORDER BY NewValue with the CASE WHEN... that´'s commented out) I know i can use ORDER

Best way to save a ordered List to the Database while keeping the ordering

爷,独闯天下 提交于 2019-11-27 05:55:39
I was wondering if anyone has a good solution to a problem I've encountered numerous times during the last years. I have a shopping cart and my customer explicitly requests that it's order is significant. So I need to persist the order to the DB. The obvious way would be to simply insert some OrderField where I would assign the number 0 to N and sort it that way. But doing so would make reordering harder and I somehow feel that this solution is kinda fragile and will come back at me some day. (I use C# 3,5 with NHibernate and SQL Server 2005) Thank you FWIW, I think the way you suggest (i.e.

mysql update a column with an int based on order

半城伤御伤魂 提交于 2019-11-27 05:38:28
问题 Lets say I have these columns uniqueID|Money|Quantity|MoneyOrder|QuantityOrder 1|23|12|| 2|11|9|| 3|99|100|| What I want to do is update MoneyOrder and QuantityOrder based on the value of ORDER BY . So the results would be: uniqueID|Money|Quantity|MoneyOrder|QuantityOrder 1|23|12|2|1 2|11|90|1|2 3|99|100|3|3 I want the update to operate like an identity column without actually making it an identity column. I know that I could just order by 'x' and the order would be the result but I want to

Linq To Sql - Dynamic OrderBy - Case When

一曲冷凌霜 提交于 2019-11-27 05:36:14
I'm using Linq to sql and Linq Dynamic OrderBy. I know linq dynamic can do simple sorting like - orderby("column_name"). But does it support something more complex like queries with "CASE WHEN" in them ? string orderbyQuery = "(CASE WHEN (username == 100) THEN 1 ELSE 0 END) DESC)"; here is my query : var u = from u in db.users orderby(orderbyQuery) select u; the above example doesn't work! , any idea if its possible? any other way to do it? thanks var u = from u in db.users orderby u.username == 100 ? 1 : 0 descending select u; a4bike This really works for me: var queryResult = from o in