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
Related
I want to ask, how to select row value when I set LIMIT 3
here my query :
SELECT
COUNT(tbl_transaction.idimport_database) AS jumlah,
product_name
FROM
tbl_transaction
GROUP BY product_name ORDER BY jumlah DESC LIMIT 3
Result :
I want to showing row query number 2, I marked Yellow.
Thanks
Simplest approach is to select only the second line:
SELECT
COUNT(tbl_transaction.idimport_database) AS jumlah,
product_name
FROM
tbl_transaction
GROUP BY product_name ORDER BY jumlah DESC LIMIT 1,1
You can use offset parameter in the limit clause to provide the offset.
SELECT
COUNT(idimport_database) AS jumlah,
product_name
FROM
tbl_transaction
GROUP BY product_name ORDER BY jumlah DESC LIMIT 1, 1
But as per your data, the above can return either of the first two rows as the value is same for both. You can provide another column in the ordering clause to specify exactly which row you'd like.
I want to select last 5 records from first 50 records in the table, currently i have following query, somebody tell me best way to select these records without calculating the limit and offset?
SELECT id FROM table WHERE enabled=1 ORDER BY date LIMIT 5, 45
Try this
SELECT id FROM (SELECT id FROM (SELECT id FROM table ORDER BY id ASC LIMIT 50) AS tbl ORDER BY id DESC LIMIT 5) as tbldata ORDER BY id ASC
this works:
SELECT id FROM (SELECT id,date FROM table ORDER BY date LIMIT 50) AS
temptable ORDER BY date DESC LIMIT 5
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
I have an table like this
I have been try this sql code
SELECT id,lat,lng,name,MIN(hitung) AS Smallest FROM open_list;
but the result give me wrong query
what i wanna do is being like this :
You could do this:
SELECT id,lat,lng,name,hitung
FROM open_list
ORDER BY hitung ASC
LIMIT 1
Or, if you want to do more complex stuff, start with this:
SELECT id,lat,lng,name,hitung
FROM open_list
JOIN (
SELECT MIN(hitung) as hitung
FROM open_list
) tmp USING (hitung)
When you do not GROUP BY non-aggregate values from your SELECT list, the returned values are arbitrary. You can add a GROUP BY but that will return multiple records, you can use ORDER BY and LIMIT to get what you're after:
SELECT id,lat,lng,name,hitung
FROM open_list
GROUP BY id,lat,lng,name
ORDER BY hitung ASC
LIMIT 1;
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