I am trying to query a table and select the rows on the current days. I am using the CURDATE() function but it doesn't seem to be working. Previously on mysql 5.1 it worked perfectly fine. But now it doesn't work.
Below is my query statement. Any help would be really appreciated. When I run this on PHP admin it brings 0 results.
When I remove the "AND DATE(checkout_date - INTERVAL 1 HOUR ) = CURDATE( )" it works fine. Thanks
SELECT * FROM `checkout` WHERE
is_confirmed = 0
AND sent_to_reminder = 1
AND DATE(checkout_date - INTERVAL 1 HOUR ) = CURDATE( )
You have a ) in the wrong place. Try
DATE(checkout_date) - INTERVAL 1 HOUR = CURDATE( )
- INTERVAL 1 HOUR needs to go outside of your function call:
SELECT * FROM `checkout`
WHERE is_confirmed = 0
AND sent_to_reminder = 1
AND DATE(checkout_date) - INTERVAL 1 HOUR = CURDATE()
Related
I am not having much knowledge on mysql queries.. and i'm trying to get values from database from last two hours from now ..I have searched about it and i have found some related posts too.. but unfortunately i am unable to implement the logic.. please help me solve this issue..
here is the mysql query
select *
from `ordermaster`
where ((
`ordermaster`.`Pick_date`
= curdate())
and (`ordermaster`.`Pick_time`
<= (now() - interval 2 hour))
and (`ordermaster`.`Status`
= 2))
were, Pick_Date = "2017-04-19" (today date) and Pick_Time = "10:00:00" (24 hours format)
Try the Following:
Select * From ordermaster om
Where Concat(om.Pick_date,' ', om.Pick_time) as date Between
(now() - interval 2 hour) and now()
AND Status = 2
You can use the between keyword
select *
from `ordermaster`
where `Pick_date` = curdate() and
`Pick_time` between (now() - interval 2 hour) and now() and
`Status` = 2
Edit
Based on our chat, where we found out the GoDaddy server you have your db hosted on is 12.5 hours ahead of your local time, the final query you should use is
select *
from `ordermaster`
where `Pick_date` = curdate() and
`Pick_time` <= now() + interval 10 hour + interval 30 minute and
`Status` = 2
I am trying to set a cron to update a status if a date has passed a value in the DB.
$q="UPDATE posts SET post_status = 3
WHERE DATE_ADD(post_date, INTERVAL post_duration DAY) < CURDATE()
";
Does not return anything run against phpmyadmin but
SELECT * FROM posts
WHERE DATE_ADD(post_date, INTERVAL post_duration DAY) < CURDATE( )
works fine? Am I missing something here
I have table ORDERS where is stored data about orders with their status and the date of order. I would like to search all orders with specified status and which was made yesterday after 3pm untill today 4pm. The query will run in different times (10am, 3pm, 5 pm... regardless).
So on example: if I run the query today (13.05.2014) I would like to get all orders made from 2014-12-05 15:00:00 untill 13-05-2015 16:00:00
The date is stored in format: YYYY-MM-DD HH:MM:SS
What I got is:
select *
from orders
where status = 'new'
and (
(
date_add(created_at, INTERVAL 1 day) = CURRENT_DATE()
and hour(created_at) >= 15
) /*1*/
or (
date(created_at) = CURRENT_DATE()
and hour(created_at) <= 16
) /*2*/
)
And I get only orders made today - like only the 2nd condition was taken into account.
I prefer not to use created >= '2014-05-12 16:00:00' (I will not use this query, someone else will).
When you add an interval of 1 day to the date/time, you still keep the time component. Use date() for the first condition:
where status = 'new' and
((date(date_add(created_at, INTERVAL 1 day)) = CURRENT_DATE() and
hour(created_at) >= 15
) /*1*/ or
(date(created_at) = CURRENT_DATE() and
hour(created_at) <= 16
) /*2*/
)
And alternative method is:
where status = 'new' and
(created_at >= date_add(CURRENT_DATE(), interval 15-24 hour) and
created_at <= date_add(CURRENT_DATE(), interval 16 hour)
)
The advantage of this approach is that all functions are moved to CURRENT_DATE(). This would allow MYSQL to take advantage of an index on created_at.
I have been looking at this statement for ages and simply cannot find the error can you guys help?
SELECT XD.*, UhED.row_class,
(SELECT id
FROM Comment C
WHERE C.Excel_Data_Excel_Lists_id = XD.Excel_Lists_id
AND C.Excel_Data_row = XD.row
LIMIT 1
) AS has_activity
FROM User_has_Excel_Lists UhXL
JOIN Excel_Lists XL
ON XL.id = UhXL.Excel_Lists_id
JOIN Excel_Data XD
ON XD.Excel_Lists_id = XL.id
LEFT JOIN User_has_Excel_Data UhED
ON UhED.Excel_Data_Excel_Lists_id = XL.id
AND UhED.Excel_Data_row = XD.row
AND UhED.User_id = 1
WHERE UhXL.User_id = 1
AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2)<-- it says that the error is here
GROUP BY XD.telephone
ORDER BY last_name ASC, first_name ASC
AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2 DAY)
try this it may help you
You forgot do specify the interval unit maybe? Something like INTERVAL 2 DAY or INTERVAL 2 HOUR maybe?
You may need to specify the measure for the interval (e.g., days, months, etc.)
You can check the syntax for DATE_SUB here
if i understand your problem like follow this instructions as:
you should must mention the day, month, hour, or year in the following line:
your code:
AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2)
change code: DAY
AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2 DAY)
if you are using the DATE(tablename) in your table you getting error like:
WRONG CODE (sample)(http://sqlfiddle.com/#!2/5b7e2/7):check the code no:5
SELECT id,DATE_SUB(DAT(NOW()), INTERVAL 2 DAY)
FROM supportContacts;
RIGHT CODE:
SELECT id,DATE_SUB(DATE(NOW()), INTERVAL 2 DAY)
FROM supportContacts;
REFER THIS LINK: http://sqlfiddle.com/#!2/5b7e2/7
I'm trying to get queries just for yesterday! With this code, I can get queries for today, but I don't know what should i do to show just yesterday queries:
$t_date = date( 'Y-m-d H:i', $_TIME - (3596 * 24) );
$twall = $db->super_query("SELECT COUNT(*) as c
FROM dle_photo_post
WHERE date > = '$t_date'
AND date < = '$t_date' + INTERVAL 24 HOUR
AND moder = '0' ");
$twall = $twall['c'];
Try this:
SELECT COUNT(*) as c
FROM dle_photo_post
WHERE date between
date_sub(curdate(), interval 2 day)
and date_sub(curdate(), interval 1 day)
AND moder = '0'