SQL using less than in to order the result - php

If there were a table like the follwing:
id title
1 a
2 b
3 c
4 d
I want to know if there is a way of selecting rows in a order by first selecting the rows with id less than 3 ordering them in id descending order, than selecting the rest in id descending order, so in this case, row 2,1,4,3.I was wondering if there was a sql statement something like this:
SELECT * FROM tablename ORDERY BY id<'3' id DESC, id DESC

You can use multiple expressions in the order by:
order by id < 3 desc,
id desc
This is close to your expression, but you have an extra id in it.

It will do the same like Gordon's answer. But I think this will make more sense for understanding.
ORDER BY CASE id < 3 THEN 1, ELSE 0 END DESC,
id DESC

Related

Sort table id's cut by a hyphen (PHP/MySQL)

I have a MySQL table that I would like to display in a decreasing way. The problem is that the ID is broken by a dash ... So MySQL does not understand my request.
SELECT * FROM table WHERE id ORDER BY id DESC
results
20633-18489
184945-190028
183661-188782
1575-1610
but I want this order
184945-190028
183661-188782
20633-18489
1575-1610
What is the solution in php?
What you are looking for is the REPLACE() FUNCTION
so your query would look like:
SELECT *, REPLACE(id, '-' , '') AS sortable_id WHERE id ORDER BY sortable_id DESC
In the SQL statement, we could do this:
ORDER BY id + 0 DESC
or this:
ORDER BY SUBSTRING_INDEX(id,'-',1) + 0 DESC
The "+ 0" is going to cause the string before it to be evaluated in a numeric context. That will return a number. And then the ordering will be by the numeric value.
As a demonstration
SELECT t.id
, t.id + 0 AS id0
, SUBSTRING_INDEX(t.id,'-',1)+0 AS id1
FROM ( SELECT '184945-190028' AS id
UNION ALL SELECT '183661-188782'
UNION ALL SELECT '20633-18489'
UNION ALL SELECT '1575-1610'
) t
ORDER BY t.id + 0 DESC
returns
id id0 id1
------------- ------ ------
184945-190028 184945 184945
183661-188782 183661 183661
20633-18489 20633 20633
1575-1610 1575 1575
Note: this is ordering only on the part of the id before the dash. This doesn't specify what order id values that "match" on the leading portion will be returned in. MySQL would be free to return these two in any order.
456-42
456-321
We can add more expressions to the ORDER BY clause to make the order of these deterministic.

ORDER BY id DESC LIMIT 3

Guys I have such datas in the table:: 1,2,3,4,5,6,7,8,9,10
query:
SELECT done_sum FROM spent_vs_done WHERE project_id="14" ORDER BY id DESC LIMIT 3
Results I have right now looks like that:
10,9,8
I would like to get the last 3 rows like:
8,9,10
Can you help me to add something to the query to achieve that.
Mysql/PHP
Any help will be Appreciated!!!!
Use a subquery to get ascending order:
SELECT t.done_sum
FROM
(
SELECT done_sum, id
FROM spent_vs_done
WHERE project_id="14"
ORDER BY id DESC
LIMIT 3
) t
ORDER BY t.id

How to reverse order output of a MySQL query

I have a basic write to and retrieve SQL database PHP "thing" and I want to have the output in descending order. How do I do that?
For example, the last entry is shown first, then the entry before that, then the entry before that, etc. The first entry ever is last.
Use:
SELECT field_name
FROM table_name
ORDER BY id DESC
By default, MySQL will show results in ascending order. If you want to show them in reverse order, use ORDER BY field_name DESC.
You can use id or date as the field name.
Change the ORDER BY statement
from ORDER BY col or ORDER BY col ASC or to ORDER BY col DESC
from ORDER BY col DESC to ORDER BY col ASC
Sort using DESC ORDER BY.
SELECT * FROM <TABLE> ORDER BY <COLUMN> DESC
If there's an auto increment field, you order by that field, descending.
SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC, DESC];
That's from ORDER BY Clause - Sort Data In SQL.
Write the below query to retrieve data:
SELECT * FROM `table_name` order by id desc
It will retrieve data from the table in descending order.
MySQL general query syntax for SELECT:
SELECT field1, field2,...fieldN FROM table_name
[WHERE Clause][GROUP BY]
[ORDER BY][LIMIT]
Example query 1:
SELECT id,first_name,last_name,email FROM users ORDER BY first_name desc // In this case you can only fetch data by = id,first_name,last_name,email
Example query 2:
SELECT * FROM users ORDER BY first_name desc // In this case all fields will be selected
Note: ORDER BY ASC = Data will sort by Ascending Order & ORDER BY DESC = Data will sort by descending Order

Select on Mysql inverse order

i have a MySql table that consists of 2 basic things:
The id and a value.
To show that on my page, i need to select, for example, the last 100 rows on reversed order.
So imagine that someone is putting data on it:
Id, value
1, 10
2, 9
3, 21
4, 15
i need, to select the last "3" rows (LIMIT + ORDER Clause), but not like this: 4,3,2 but like this: 2,3,4.
I know how to do that on code, but maybe there is a simple solution for that on Mysql and i don`t know.
Thanks
My SQL Query is like this right now:
SELECT `Data`.`id`, `Data`.`log_id`, `Data`.`value`, `Data`.`created` FROM `control_panel`.`datas` AS `Data` WHERE `Data`.`id` > 1000 AND `Data`.`log_id` = (2) ORDER BY `Data`.`id` DESC LIMIT 100
You need to wrap the first ORDER BY in a subselect which will return a limited selection ordered in descending order, then you can order that result in the outer query in ascending order:
SELECT
a.*
FROM
(
SELECT id, value
FROM tbl
ORDER BY id DESC
LIMIT 3
) a
ORDER BY
a.id
One way to do this would be with a sub-select.
SELECT *
FROM (SELECT * FROM table_name ORDER BY id DESC LIMIT 3) tmp
ORDER BY id ASC
simply
SELECT t.*
(SELECT * FROM table_name
ORDER BY column_name DESC
LIMIT 0,3) t
ORDER BY t.column_name ASC
use DESC to descending order, ASC to increasing order

mysql row counter

I have a mysql table. It has auto increment on the id. but I regularly delete rows so the numbers are all over the place. I need to get the last n rows out, but because of deletions, the common way of using the max of the autoincremented id column doesn't work well...
1 - Is their another way to get the bottom 50?
2 - Is their a way to get rows by actual row number? so if I have 4 rows labelled 1,2,3,4 delete row 2 then it will become 1,2,3 rather than 1,3,4?
SELECT ... ORDER BY id DESC LIMIT 50
SELECT *
FROM TABLE
ORDER BY id DESC
LIMIT 50
EDIT
To pick the last 50, but sort by id ASC
SELECT X.*
FROM ( SELECT *
FROM TABLE
ORDER BY id DESC
LIMIT 50
) X
ORDER BY X.id
1 - First get total row count like
SELECT COUNT(*) AS c FROM ...
then use
SELECT ..... LIMIT [start],[count]
2 - One idea is to use view , or procedure, but this is much more harder and may be used when there is no other way to avoid this
1 - Is their another way to get the bottom 50?
SELECT * FROM table_name ORDER BY record_id DESC LIMIT 50
2 - Is their a way to get rows by actual row number? so if I have 4 rows labelled 1,2,3,4 delete row 2 then it will become 1,2,3 rather than 1,3,4?
SELECT * FROM table_name
1 - Yes but it is ugly afaik, you do a
SELECT whateveryouwant FROM table ORDER BY yourprimarykey DESC LIMIT 50
the you fetch the rows into an array and reverse the array, in php :
$r = mysql_query('SELECT * FROM table ORDER BY primarykey DESC LIMIT 50');
$set = array();
while($row = mysql_fetch_assoc($r)) $set = $row;
$set = array_reverse($set);
foreach($set as $row) {
// display row ...
}
2 - You'll have to manage your primary key by yourself, its a bit risky ...

Categories