MySQL Order rows by date - mysql

I'm trying to order my rows by the date that each row has.
One row has 2016-09-15 15:36 and one has 2016-08-15 13:12 How would i order them so that the highest one is at top?
Their row is called th_activity and i know that in a normal query, you would do something like SELECT * FROM threads ORDER BY th_activity DESC but i believe i know the problem. I use varchar as the type for the row. I'm not really sure what the length/value should be for date if that's what i must use.
If someone can explain how i would order this properly, i'd appreciate it.

If your date is year-month-day hour:minute, you can order alphabetically without any problems, so ORDER BY th_activity DESC should work fine.

You could have set the type of column as timstamp instead of varchar, which doesn't care about the length of data (only that while inserting data take care about the format).
Check this question to ordery by timestamp:
MYSQL - Order timestamp values ascending in order, from newest to oldest?
SELECT th_activity
FROM tableName
ORDER BY timestamp desc;

could be you need a str_to_date conversion
select * from threads
ORDER BY str_to_date(th_activity, '%Y-%m-%d %h:%i') DESC

I would use a data type
datetime
and use the same select you wrote in the question :
order by th_activity DESC

Related

Get latest record from database based on month and date

I'm trying to get the latest record from a table based on the date. My date format is mm/yyyy. I've tried using MAX(date) but it's getting the record based on the month only. I've also tried using MONTH(MAX(date)) but returns a null value. Any help is much appreciated. By the way, I'm currently using XAMPP if that helps.
Dates don't have formats. In MySQL, you can use:
select t.*
from t
order by right(date, 4) desc, left(date, 2) asc
limit 1;
You should learn to use the built-in data types for dates and date/times.
Try parsing the string format to date format before filtering, you can refer to this post:
how to convert a string to date in mysql?
Did you try with :
SELECT *
FROM tables
[WHERE conditions]
ORDER BY expression DESC;
The order expression will be date field name you want to order.

Applying dual conditions in case when in MYSQL

(question edited) My table has id,status,date,sequence
Situation: Get ids which satisfies:
date is max, not more than today's and status is A
if more than 1 status' with same date then get id only if it has max sequence
I am writing MYSQL in dbVisulizer.
edit: trying with query:
select id, max(date), max(sequence) from
table
where
table.date<=now() and status='A'
order by date desc,sequence desc
It sounds like I am just asking direct question without trying anything by myself, but for this situation I am completely stuck, I tried with case when but couldn't really accomplish any good, any starting point would be appriciated.
select id, max(date), max(sequence) from
table
where
table.date<=now() and status='A'
order by date desc,sequence desc
group_by id;
This should give you the desired results. Adapt table and field names to your table and fields.
select id from
table
where
table.date<now() and table.status='A'
order by date desc,sequence desc
limit 1;
You should check it for mistakes by yourself.

MySQL - Retrieve 7 most recent timestamps, sorted oldest first

As above, I'm unsure on how to do it, although I am possibly overlooking something simple
I want to retrieve the timestamps and another column, just the most recent 7 which I have done using LIMIT and ordered them using ORDER BY timestamp DESC to get the most recent 7... But once retrieved, I'd like them oldest first rather than newest first
Anyone able to assist please?
Thanks!
two selects could work in this case. it would at least be one possible way to achieve what you want. i'm not sure if it would the best way.
i'm assuming your table has an id field.
select * from records
where id in(select id from records order by timestamp desc limit 7)
order by timestamp asc;
this lets you get the latest 7 rows in the inner select, then sort them in ascending order.
A nested query should take care of this. Something like
SELECT *
FROM (
SELECT timestamp, anotherColumn
FROM tableName
ORDER BY timestamp DESC
LIMIT 7 )
ORDER BY timestamp ASC;

Order By on date field starting in a middle point of the dates range

I have a table "A" with a "date" field. I want to make a select query and order the rows with previous dates in a descending order, and then, the rows with next dates in ascending order, all in the same query. Is it possible?
For example, table "A":
id date
---------------------
a march-20
b march-21
c march-22
d march-23
e march-24
I'd like to get, having as a starting date "march-22", this result:
id date
---------------------
c march-22
b march-21
a march-20
d march-23
e march-24
In one query, because I'm doing it with two of them and it's slow, because the only difference is the sorting, and the joins I have to do are a bit "heavy".
Thanks a lot.
You could use something like this -
SELECT *
FROM test
ORDER BY IF(
date <= '2012-03-22',
DATEDIFF('2000-01-01', date),
DATEDIFF(date, '2000-01-01')
);
Here is a link to a test on SQL Fiddle - http://sqlfiddle.com/#!2/31a3f/13
That's wrong, sorry :(
From documentation:
However, use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows. Therefore, the use of ORDER BY in this context is typically in conjunction with LIMIT, so that it is used to determine the subset of the selected rows to retrieve for the SELECT, even though it does not necessarily affect the order of those rows in the final UNION result. If ORDER BY appears without LIMIT in a SELECT, it is optimized away because it will have no effect anyway.
This should do the trick. I'm not 100% sure about adding an order in a UNION...
SELECT * FROM A where date <= now() ORDER BY date DESC
UNION SELECT * FROM A where date > now() ORDER BY date ASC
I think the real question here is how to do the joining once. Create a temporary table with the result of joining, and make the 2 selects from that table. So it will be be time consuming only on creation (once) not on select query (twice).
CREATE TABLE tmp SELECT ... JOIN -- do the heavy duty here
With this you can make the two select statenets as you originally did.

How do I select a record whose timestamp is the most recent

I have a mysql db which one of the columns is of type timestamp. And the records look like this
How do I select the most recent timestamp in a query and display it?
SELECT * FROM tbl ORDER BY created_ts DESC LIMIT 1
PS: you should use hashed passwords rather than plain-text ones
It's simple:
SELECT * FROM TABLE ORDER BY CREATED_TS LIMIT 1
I guess its better to index the sequence ID and get the max from there. Getting max of your timestamp will run into real performance issues when your table grows big.