Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move one row to the end of a result set in MySQL

Tags:

mysql

I would like to move a row to the bottom of the result set given a matched condition.

Database

+-------+------------+
|Symbol | Percentage |
|-------|------------|
|VG     | 20         |
|-------|------------|
|CASH   | 20         |
|-------|------------|
|GOOG   | 60         |
+-------+------------+

ex: SELECT * FROM TableName -SEND TO END OF RESULT SET- WHERE symbol = 'CASH'

result set:

GOOG
VG
CASH

To clarify my original question...

I need to write an exception for an ORDER BY statement. To put the query into plain english - SELECT an entire row, ordering by a timestamp, except if the symbol is "CASH"

like image 426
Derek Adair Avatar asked Aug 30 '10 22:08

Derek Adair


People also ask

How do I move a row in MySQL?

You can move rows from one table to another with the help of INSERT INTO SELECT statement.

How do I change the position of a row in SQL?

I thought of mixing a bit of SQL with my server-side language, eg. (moving up Frank): if (newPosition > oldPosition) { UPDATE people SET position = position - 1 WHERE listId = 1 AND position <= @newPosition AND Name != "Frank"; UPDATE people SET position = @newPos WHERE listId = 1 AND Name="Frank"; } else { … }

How do I select a specific row in MySQL?

MySQL SELECT specific rows When a user wants to retrieve some individual rows from a table, a WHERE clause has to be added with the SELECT statement immediately followed by a condition. Here * indicates all columns.

How do I select the last one row in SQL?

To select the last row, we can use ORDER BY clause with desc (descending) property and Limit 1.


1 Answers

To change the order of the rows in a result set you should use ORDER BY:

SELECT *
FROM TableName
ORDER BY symbol = 'CASH', timestamp
like image 56
Mark Byers Avatar answered Oct 23 '22 23:10

Mark Byers