MYSQL UPDATE WITH DATE_ADD - mysql

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

Related

Find records from MySQL which is updated within last hour but not created within last hour

I want to query from MySQL where posts will come only which is updated within last hour but not created in last hour.
Here is my MySQL query:
SELECT *
FROM
(SELECT *
FROM posts
WHERE (updated_at >= (NOW() - INTERVAL 30 MINUTE))) AS A
WHERE (created_at < DATE_SUB(NOW(), INTERVAL 60 MINUTE))
But this query doesn't give me right result.
I am attaching sample posts in CSV and adding result output from this query.
Sample: []
Query Output: []
I think you just need both conditions in a WHERE clause:
SELECT *
FROM posts
WHERE
updated_at >= DATE_SUB(NOW(), INTERVAL 1 HOUR) AND
created_at < DATE_SUB(NOW(), INTERVAL 1 HOUR);

How to select values from 2 hours ago from now

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

Get all rows before a specific day

We are using the below updated SQL to get customers list from our db whom we send SMS before 3 days.
SELECT * FROM sms WHERE sent_time >= NOW() - INTERVAL 3 DAY;
The table sms is updated daily along with the sent_time column with default value of 0 or the last sent time.
There are rows with the value of sent_time = 0 but no row is fetched by the above script.
What is the correct SQL?
Earlier we were using the SQL with php like mentioned below:
$vTime = time() - ( 60*60*24*3 );
$sql = "SELECT * FROM sms WHERE $vTime <= sent_time";
The function NOW() will return current date and time, but as I can see you have used PHP time() before, which returns a Unix-Timestamp. The SQL equivalent is UNIX_TIMESTAMP().
Syntax UNIX_TIMESTAMP()
SELECT * FROM sms WHERE sent_time >= UNIX_TIMESTAMP() - (60*60*24*3);
Syntax UNIX_TIMESTAMP(date)
SELECT * FROM sms WHERE sent_time >= UNIX_TIMESTAMP(NOW() - INTERVAL 3 DAY) OR sent_time = 0
NOW() - INTERVAL 3 DAY; returns a DATETIME while echo time() - ( 60*60*24*3 ); returns a timestamp.
If your database column is a timestamp, your MySQL test will never work, use this instead:
SELECT * FROM sms WHERE sent_time >= UNIX_TIMESTAMP(NOW() - INTERVAL 3 DAY)
Just change select query..
SELECT * FROM sms WHERE sent_time >= DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW();
Try below:
SELECT * FROM sms WHERE sent_time <= DATE_SUB(NOW(), INTERVAL 3 DAY) OR sent_time=0;

Date function not working after updrading to mysql 5.7

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()

MYSQL Select By This Week

I would like to have user can see their uploaded images by this week.
Is this correct?
"SELECT *
FROM images
WHERE userid = '$userid'
AND uploadeddate >= DATEPART(week, uploadeddate) = DATEPART(week, GETDATE())
ORDER BY uploadeddate DESC";
I am getting error. Thanks for the help.
SELECT *
FROM images
WHERE userid = $userid
AND uploadeddate >= CURDATE() - INTERVAL WEEKDAY(day) DAY
AND uploadeddate < CURDATE() - INTERVAL WEEKDAY(day) DAY + INTERVAL 7 DAY
Create an index on (userid, uploadeddate) for this to work fast.
The below queries will return records for the current week. It's also best practice to specify a column list in your SELECT.
If you're using SQL Server:
SELECT ... WHERE DATEPART(week, uploadeddate) = DATEPART(week, GETDATE())
If you're using MySQL:
SELECT ... WHERE WEEK(uploadeddate) = WEEK(CURDATE())