How can I order this MySQL Date? - php

I have a problem ordering a mysql result by Date. The date is in this format: 15:24:57 - 21/04/2019 (24 hour clock)
I've tried this:
SELECT PlayerSteamID, BanLength, BanReason, AdminName, AdminSteamID, PlayerName, MapName, DateAndHour FROM BansList ORDER BY DateAndHour DESC
and this
SELECT PlayerSteamID, BanLength, BanReason, AdminName, AdminSteamID, PlayerName, MapName, DateAndHour FROM BansList ORDER BY UNIX_TIMESTAMP(STR_TO_DATE(DateAndHour, '%h:%i:%s - %d/%m/%Y')) DESC
But doesn't seem to work...
How can I make it so it's ordered by that date & time?

assuming your DateAndHour column is a string could be you need a proper conversion
SELECT PlayerSteamID
, BanLength
, BanReason
, AdminName
, AdminSteamID
, PlayerName
, MapName
, DateAndHour
FROM BansList
ORDER BY str_to_date(DateAndHour, '%T - %d/%m/%Y') DESC

Related

MySQL; ORDER BY STR_TO_DATE doesn't work vice versa

This is my code:
SELECT id, title, date FROM table ORDER BY STR_TO_DATE(date,"%m/%d/%Y %h:%i:%s")
The date output is:
2016-12-11 14:40:00
2016-11-15 08:50:09
2016-11-15 08:54:58
SELECT id, title, date FROM table ORDER BY STR_TO_DATE(date,"%m/%d/%Y %h:%i:%s") DESC
doesn't work for me for some reason.
How can I reach to ORDER BY this?
2016-11-15 08:54:58
2016-11-15 08:50:09
2016-12-11 14:40:00
Edit: date is stored as timestamp in my MySQL database!
Assume you want the data that order by date ascending and time descending.
You can try this:
SELECT id, title, date
FROM table
ORDER BY DATE(date) ASC , TIME(date) DESC

How can I ORDER the result after i've used LIMIT?

This is kind of straight forward.
I want to ORDER BY date DESC Limit 4 and then I want to ORDER BY date ASC on that result, so just 4-games from the middle of the big table with ASC date order, any ideas?
Just ORDER BY date ASC Limit 4 Does not work!
What I have:
What I get:
What I want:
you can use subquery :
SELECT a.* FROM (SELECT * FROM yourtable ORDER BY date DESC Limit 4) a ORDER BY a.Date
if you want to get the last four rows from table
select * from table order by date desc limit 0,4
if you want to get the first four rows from table
select * from table order by date asc limit 0,4

Sort clanwars by date, without showing before today

I want to sort clanwars by date, without showing the clanwars that are before today (showing the clanwars of today too, ">="), but I don't know how, I'm using webspell (CMS) and don't know how to do it. This is the code:
$ergebnis=safe_query("SELECT * FROM ".PREFIX."clanwars ORDER BY date ASC LIMIT 0, ".$maxresults);
Any help will be appreciated.
$ergebnis=safe_query(
"SELECT *
FROM ".PREFIX."clanwars
WHERE `date` > DATE(NOW())
ORDER BY `date` ASC LIMIT 0, ".$maxresults);
NOW() returns the current date and time, DATE() returns just the date part, which is equivalent to 2013-09-12 00:00:00.
Try this:
$ergebnis=safe_query("SELECT * FROM ".PREFIX."clanwars WHERE date >= curdate() ORDER BY date ASC LIMIT 0, ".$maxresults);
Edit: convert date field to MySQL date format
$ergebnis=safe_query("SELECT * FROM ".PREFIX."clanwars WHERE STR_TO_DATE(date, '%d.%m.%Y') >= curdate() ORDER BY date ASC LIMIT 0, ".$maxresults);

How can I order by date?

I have a problem with a query (mysql).
I have to order by date ASC tha problem is that I also have empty date in the table. Mysql set them to 0000-00-00, so when I order by date those dates are shown first, but i need to put the "empty dates" at the end, how can I do?
Example:
0000-00-00
2011-01-01
2010-12-12
Query: .... ORDER BY date ASC
Results should be:
2010-12-12
2011-01-01
0000-00-00
Thanks
ORDER BY IF(date = '0000-00-00', 1, 0) ASC, date ASC
Assuming you're doing:
SELECT *
FROM table
ORDER BY date ASC
you could do
SELECT *, IF(date > 0, 1, 0) AS has_date
FROM table
ORDER BY has_date ASC, date ASC
I believe that would do it for you.

how to select the 5 latest row from my mysql

I wanted to know the sql command to retrieve 5 latest row from my table? Below is my sql query. How am I going to do, in order it can select the 5 latest row base on row no to be process?
$query =
"SELECT lat, lng, DATE_FORMAT(datetime,'%W %M %D, %Y %T') AS
datetime FROM markers1 WHERE 1";
You need to order the results, and set a limit.
Assuming datetime is what you wanted to order on:
$query = "
SELECT
lat,
lng,
DATE_FORMAT(datetime,'%W %M %D, %Y %T') AS datetime
FROM markers1 WHERE 1
ORDER BY datetime DESC
LIMIT 5
";
EDIT:
To answer OP's comment: "result that i get is start for Row 50 for first query and it follow by 49,48,47,46 Is that possible i can get this start frm row 46,47,48,49,50 ?"
You could either do this with the result in PHP, by fetching the rows and storing them in an array and reversing the array. I don't believe you can efficiently loop through a mysql result resource in reverse.
To do this in the SQL query, you need to create a temporary table with the original query:
$query = "
SELECT
lat,
lng,
DATE_FORMAT(datetime,'%W %M %D, %Y %T') AS datetime
FROM (
SELECT
lat,
lng,
datetime
FROM markers1 WHERE 1
ORDER BY datetime DESC
LIMIT 5
) AS tmp_markers
ORDER BY datetime ASC
";
The result of the initial query is used as the table to search in a new query, that orders by datetime ascending. I had to apply the DATE_FORMAT on the outer query because we need the datetime field to order by again.
just add:
ORDER BY datetime DESC
LIMIT 5
The best way to do this is to add a row number and then reverse your query based on that, since we don't have any guarantees that your datetime field increments chronologically; and I'm also assuming that by 5 latest rows you mean the last 5 added to the table, not the 5 with the most recent datetime.
SELECT
lat
, lng
, DATE_FORMAT(datetime,'%W %M %D, %Y %T') AS datetime
, #rownum:=#rownum+1 `RowNum`
FROM
markers1
, (SELECT #rownum:=0) `r`
WHERE 1
ORDER BY RowNum DESC
LIMIT 5
Check out this dude's blog for the rownumber solution I used.
Might be a very late answer, but this is good and simple.
select * from table_name order id desc limit 5
This query will return a set of last 5 values(last 5 rows) you 've inserted in your table
Check out my query for last 5 entry
SELECT * FROM notices where organizationid = $orgid ORDER BY `notices`.`id` DESC LIMIT 5
So, Most Important is ORDER BY notices.id DESC LIMIT 5

Categories