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.
Related
SELECT c.enddate FROM cohort c ORDER BY c.enddate DESC LIMIT 1
I have a sql query above, it works in database, the result select a date like: '2018-07-18'
I try to use while loop with mysqli_fetch_row in php to fetch this date, but the result will only fetch:
2018
How can I get the whole date?
if ($runquery = $conn->query($result_validation))
{
//get the enddate from the last cohort
while ($row = mysqli_fetch_row($runquery))
{
$lastDate = $row[0];
}
}
$row[0] only display first number: 2018.
Adding DATE() function in the column will convert it to a valid date format like for the example below!
SELECT DATE(c.enddate) FROM cohort c ORDER BY DATE(c.enddate) DESC LIMIT 1
Try SELECT DATE(c.enddate) FROM cohort c ORDER BY c.enddate DESC LIMIT 1,
Try this one
SELECT DATE_FORMAT(c.enddate,"%Y-%m-%d") FROM cohort c ORDER BY c.enddate DESC LIMIT 1
The DATE_FORMAT() function formats a date as specified by a format mask.
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
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
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);
I am trying to make a query about joining showingdates and showings table and get all the showings for a date and order them by time.
My showingdates dayfield is formed as showdate(datetime) and shows "2013-03-06 00:00:00"
My showings performance_date field is again a datetime and examples are
"2013-03-06 20:00:00 ,
"2013-03-06 21:00:00" .
How can I make a query for that date and order them by time ?
This should work.
SELECT garbage
FROM trash
WHERE DATE(showdate) = '2013-03-06'
ORDER BY TIME(showdate) DESC
The time here is descending. Remove DESC to achieve ascending order.
To get rows only from a specific date (no use of any function on the showdate column, so indexes can be used):
WHERE showdate >= '2013-03-06'
AND showdate < '2013-03-06' + INTERVAL 1 DAY
To order by time (since all rows will have the same date, and the showdate has both date and time, simply order by that):
ORDER BY showdate