I'm trying to order some MySQL records by DESC order and get the last record added.
These are my records and they're all stored as strings:
1/2017
1/2018
2/2017
2/2018
3/2017
I want to get the value 1/2018. The records are based on the current year
and the last record added was 1/2018 and the next will be 2/2018 and so on.
this is my query:
SELECT NoControl FROM cita ORDER BY NoControl DESC LIMIT 1
but I'm getting 3/2017 as the last record.
If you have ever the same pattern m/yyyy you could use some string manipulation function eg:
SELECT NoControl
FROM cita
ORDER BY right(NoControl,4), DESC left(NoControl,1) DESC LIMIT 1
But you should avoid of store date values as string ..
or convert the string as a proper date
SELECT NoControl
FROM cita
ORDER BY str_to_date(NoControl,'%m/%Y') DESC LIMIT 1
Use SUBSTRING_INDEX to extract the first and second part, CAST as integer and sort:
SELECT str
FROM testdata
ORDER BY
CAST(SUBSTRING_INDEX(str, '/', -1) AS UNSIGNED) DESC,
CAST(SUBSTRING_INDEX(str, '/', 1) AS UNSIGNED) DESC
LIMIT 0, 1
SQL Fiddle
Related
I need to get data from MySQL table in ascending order but zero values come last and randomly
Right now this is my order by condition.This is not working
ORDER BY sortorder=0 RAND(),sortorder
Use conditional ordering
select *
from table
order by column > 0 desc, column asc, rand()
Add rand() at the end
Demo
Or you could use union
(select * from table where column > 0 order by column asc)
union all
(select * from table where column = 0 order by rand())
Demo
If you want to get data from mysql randomly and zero values come last. You may want to try the following:
SELECT * FROM table ORDER BY `column` = 0, rand();
So, I've written a query which should grab 15 most recent results from the 'messages' table, but order results by date in descending direction. My current query is as follows:
SELECT * FROM messages
WHERE chatID = 1
ORDER BY ID DESC, timeSent ASC
LIMIT 15
As you can see, I am using the 'ID DESC' to get the 15 most recent results, but the 'timeSent ASC' isn't ordering the results in the order I wish.
How can I correct my query to achieve this?
First fetch the messages by ordering the ID then sort it according to timeSent. You can try this -
SELECT * FROM
(SELECT * FROM messages WHERE chatID = 1 ORDER BY ID DESC LIMIT 15) messages_ordered
ORDER BY timeSent ASC
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
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
Suppose i have a variable ($var) that determines what rows get selected for an mysql query.The table to be searched has a date column along with some others.
If the value of $var = 1 then retrieve top 5 rows in desc date order.
If the value of $var = 2 then retrieve rows 6-10 in desc date order.
If the value of $var = 3 then retrieve rows 11-15 in desc date order.
SELECT
*
FROM
my_table
ORDER BY
my_table.date DESC
LIMIT
[($var - 1) * 5], 5
Where [] is where you should embed your PHP using . to concatenate strings if $var is between 1 and 3
This is a simple as using the OFFSET syntax in the mysql SELECT statement:
SELECT * FROM myTable ORDER BY date DESC LIMIT ($var*5, ($var-1)*5 +1)
select * from table order by date desc limit (($var-1)*5+1), ($var*5)