I am creating a website that lists upcoming events, events are saved in a DB.
I am trying to only show events that are happening today or in the future, not in the past
here is my code:
("SELECT * FROM events where deleted='0' AND date > CURDATE() ORDER by STR_TO_DATE(date, '%d/%m/%Y') ASC ")
However this doesn't seem to show anything at all, can anyone help?
Thanks
The field date seems to be in the '%d/%m/%Y' format and is not defined as date, datetime, or timestamp data type.
Change:
SELECT * FROM events
where deleted='0'
AND date > CURDATE()
ORDER by STR_TO_DATE(date, '%d/%m/%Y') ASC
To:
SELECT * FROM events
where deleted='0'
AND STR_TO_DATE( date, '%d/%m/%Y' ) >= CURDATE()
ORDER by
STR_TO_DATE( date, '%d/%m/%Y' ) ASC
Related
I'm trying to query only distinct dates from my table (ignoring the times) which uses timestamp for the date format (should I use a better format?). Here is my query, but it doesn't seem to work:
$query = "
SELECT DISTINCT DATE(event_date)
FROM schedule
WHERE DATE(event_date) >= CURDATE()
ORDER BY event_date ASC LIMIT 4
";
"event_date" is my timestamp row in the database.
You may have a problem with the order by. How about this?
SELECT DATE(event_date)
FROM schedule
WHERE event_date >= CURDATE()
GROUP BY DATE(event_date)
ORDER BY DATE(event_date) ASC
LIMIT 4;
i got a table that stores start_date and end_date with the field type DATE
I want to retrieve all records between the start_date and end_date but my below SQL statement do not give any results.
$sql = "SELECT * FROM expo Where date >= DATE(NOW()) AND end_date <= DATE(NOW())";
You can use the between function to perform this action.
SELECT *
FROM expo
WHERE NOW() BETWEEN date AND end_date
Oh, since your column is date, not datetime use date(now()). That will select records that start and end on the current date as well.
Demo: http://sqlfiddle.com/#!9/0aaad/6
SELECT *
FROM expo
WHERE date(NOW()) BETWEEN date AND end_date
Use curdate
SELECT *
FROM expo
WHERE date >= curdate() AND end_date <= curdate()
I am working on an event management project, I need to get upcoming event from database, can anyone help me ?
I am using
SELECT *FROM EVENTS WHERE DATE_ADD(event_date, INTERVAL YEAR(CURDATE())-YEAR(event_date) YEAR)
BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY) LIMIT 1;
But it gives me the event upcoming within 7 days only.
I should not want to provide days.
I want to get first upcoming event , doesn't matter after or within how many days it is coming.
thank you !
Isn't this a simple where clause comparing with current date
SELECT *
FROM EVENTS
WHERE event_date > CURDATE()
ORDER BY event_date
LIMIT 1;
Wouldn't this work?
SELECT * FROM EVENTS WHERE event_date > CURDATE() ORDER BY event_date ASC LIMIT 1;
try
If column type datetime you can use CURDATE()(mysql function) or date()(php function)
"SELECT *FROM EVENTS WHERE event_date > '".date('Y-m-d H:i:s')."' ORDER BY event_date limit 1"
or
"SELECT *FROM EVENTS WHERE event_date > CURDATE() ORDER BY event_date limit 1"
If column type date
"SELECT *FROM EVENTS WHERE event_date > '".date('Y-m-d')."' ORDER BY event_date limit 1"
try below it works perfectly fine
select * from table where start_time between NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY)
you can use like this to get next 7 days data or in case you want last 7 days then instead of DATE_ADD USE DATE_SUB It will work or you can use like
start_time between NOW() AND DATE_ADD(NOW(), INTERVAL 1 WEEK)
this also works. to know more check this ref https://dev.mysql.com/doc/en/date-and-time-functions.html
thank you everyone,
I got the answer:
SELECT * FROM EVENTS
WHERE event_date > CURDATE()
ORDER BY event_date ASC LIMIT 1;
Here is my query i am retrieving birthday list from database using this query i want to retrieve data in asc order by dob(date of birth) i am using order by dob asc but its giving mysql syntax error.
QUERY
SELECT * FROM members
WHERE DATE_ADD(dob, INTERVAL YEAR(CURDATE())-YEAR(dob) YEAR)
BETWEEN CURDATE()
AND DATE_ADD(CURDATE(), INTERVAL 7 DAY
ORDER BY dob ASC
its working now here is one problem again the dob(date of birth) format is 1990-10-11 when i am using order by dob asc its short the data 1990-10-11 , 1991-10-09, but i want to short this by data not year like 1991-10-09, 1990-10-11
try this you are missing ) in your query
"SELECT * FROM members WHERE DATE_ADD(dob, INTERVAL YEAR(CURDATE())-YEAR(dob) YEAR) BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY) order by date(dob) asc"
if you wan to sort by month than use order by Month(dob)
You can also try with selected field something like this
'SELECT field1, field2, DATE_FORMAT(dob, "%d-%M-%Y") AS userdob FROM members WHERE
DATE_ADD(dob, INTERVAL YEAR(CURDATE())-YEAR(dob) YEAR) BETWEEN CURDATE() AND
DATE_ADD(CURDATE(), INTERVAL 7 DAY) order by userdob asc'
You missed a )
...BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY order by...
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);