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;
Related
I am working on booking website. I've booking table with date_created and time_created field. I am storing todays date as date_created and current time as time_created. I want to list the booking after 15 minutes of time_created in admin panel. How this can be done?
I'd tried this query.
SELECT *
FROM tbl_booking
WHERE (date_created = '$todaysdt'
AND time_created >= '$timenow')
OR (date_created < '$todaysdt')
ORDER BY booking_id DESC
but this not working. Any help will be appreciated. Thanks in advance.
Give this a shot:
select *
from tbl_booking
where STR_TO_DATE(concat_ws(date_created," ",time_created),"%Y-%m-%d %H:%i") < DATE_SUB(NOW(), INTERVAL 15 MINUTE)
order by booking_id desc
i think may be you need this: UPDATED
select * from tbl_booking where (date_created = CURDATE() AND time_created <= time(DATE_SUB(NOW(), INTERVAL 15 MINUTE))) OR (date_created < CURDATE()) order by booking_id desc
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 have this query
SELECT * FROM content
WHERE topic='$rw09[id]' AND active='1' AND date < DATE_SUB(CURDATE(), INTERVAL 1 WEEK)
ORDER BY cpc DESC, id DESC
LIMIT 4
The key part of my query is date < DATE_SUB(CURDATE(), INTERVAL 1 WEEK). It returns me entries older than a week. What i want it to return me is entries NOT older than 1 week. How can i modify it to return me desired result?
Thank you.
Have you tried with
SELECT * FROM content
WHERE topic='$rw09[id]' AND active='1' AND date > DATE_SUB(CURDATE(), INTERVAL 1 WEEK)
ORDER BY cpc DESC, id DESC
LIMIT 4
?
I am trying to call data from SQL table that is only 3 days old
My table has a lbs-date column in it and is date format. I have tried the following but get no result from the query at all
$result = mysql_query("SELECT *, DATE_FORMAT(datetime, '%y,%m,%d') FROM lbs_trace_etrack
WHERE lbs_date(datetime) = CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC")
Is there any other way I can call only the last 3 days of information from the SQL my date format is Y/M/D
SELECT *, DATE_FORMAT(lbs_date, '%y,%m,%d')
FROM lbs_trace_etrack
WHERE lbs_date >= CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC
check DATE_FORMAT. Its syntax is DATE_FORMAT(<date>,format) . Use like this :
SELECT *, DATE_FORMAT(lbs_date , '%y,%m,%d') FROM lbs_trace_etrack
WHERE lbs_date = CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC
I am in problem in mysql database data retrieving. I have to get latest data inserted within a week or latest 7 days. I just know get data of specific date, but no within a span of days.
Please anyone help me. I am new in mysql.
You are looking for INTERVAL. For example, this will find all users whose created_time is in last 7 days and you have field created_time to tracked date of creation of record
SELECT * from users where created_time > (NOW()-INTERVAL 7 DAY)
You can do it using below query.
SELECT *
FROM `table`
WHERE `date` BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND
CURDATE()
OR
SELECT * FROM `table` WHERE `date` >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
OR
SELECT * FROM table WHERE DATEDIFF(NOW(),dateField) < 7
we can use DATE_SUB Mysql default function
select * from yourtable where date_of_insertion >= DATE_SUB(NOW(),INTERVAL 7 DAY)
<?php
$date = date("your_date_format", strtotime(- 7 days));
$result = mysqli_query($link, "SELECT * FROM table WHERE `date` > '$date'");
?>
Simple as that.
try :
select * from tablename where add_time >= now() - interval 7 day