I tried every possible bracket combination but I just can't get this line to work!
I also tried looking for the solution but I can't seem to find anything similar. I hope someone can shed some light on my ignorance ^^.
UPDATE contact_info SET birthday= CURDATE() - TIME_TO_SEC(NOW()) % 50000 WHERE contactID=1;
Thanks in advance!
You need to convert the expression you calculate to an INTERVAL specifying the number of days. Then use DATE_SUB to subtract the interval from the current date.
UPDATE contact_info
SET birthday = DATE_SUB(CURDATE(), INTERVAL (TIME_TO_SEC(NOW()) % 50000) DAY)
WHERE contactID = 1;
Related
Can anybody please tell me how to add a yesterday time stamp whenever my table get updated? Currently, it giving me today date instead yesterday date. Please see below picture.
I tired adding (CURRENT_TIMESTAMP, -1) on default/Expression. Did not work.
Comment Picture
Thank you so much
You can use SUBDATE(CURRENT_DATE, INTERVAL 1 DAY) to subtract one day from today's date. For example:
SELECT *
FROM table_name
WHERE DATE(Date) = SUBDATE(CURRENT_DATE(), INTERVAL 1 DAY)
UPDATE
To update the value in the database, you can do the following:
UPDATE table_name
SET date = SUBDATE(CURRENT_DATE(), INTERVAL 1 DAY)
WHERE column = 'value'
I also needed the same, to update yesterday date whenever my table gets data. I have scheduled the following query, which worked perfectly for me.
I hope it will work for you also:
sql_yesterday_date = 'UPDATE tbl_dailytrade SET Date = subdate(current_date, 1) WHERE Date(date) = CURDATE()'
I'm working on a small project and I've run into some trouble.
The issue is that I want to check if there has been more than 30 minutes since a u_latest_activity latest was updated, if there's been, then update and set u_online=0
The field in the MySQL database is a datetime field.
I have no PHP code to show yet since none that I've tried has worked, I tried using the DateTime object but I'm clueless about how it works. I googled alot but I still couldn't figure it out so I use this as a last resort. I'll gladly accept a SQL solution as well.
Thanks in advance.
This should do it:
UPDATE your_table SET u_online = 0 WHERE u_latest_activity < DATE_SUB(NOW(), INTERVAL 30 MINUTE)
this is pure mySQL ans might help
SELECT if(DATE_ADD(u_latest_activity, INTERVAL 30 MINUTE) > NOW() , 1 , 0) as u_online from mytable;
I am trying to update a DATETIME field in a table. What I want for it is to always have three hours before it expires.
My current code is:
UPDATE mindcore_sessions
SET session_time = DATE_ADD(session_time, INTERVAL 3 HOUR)
WHERE session_id = '$sessionId';
An example value would be: 2013-02-11 00:00:00. If I run this query, it will change to: 2013-02-11 03:00:00. Which is correct. But, if it is run again, it changes to 2013-02-11 06:00:00 and so on.
What I want for it is to always be only three hours ahead. This is hard to explain, but I just want it to stay the same if it is run again. And if it is run again 1 minute later, I want it to increment by just one minute.
Sorry if the question is verbose, but I am utterly confused (and it is late!).
Thanks :)
Instead of adding three(3) hours to the last session_time, add three(3) hours to NOW():
update mindcore_sessions
set session_time = DATE_ADD(NOW(), INTERVAL 3 HOUR)
where session_id = '$sessionId';`
Add your interval to NOW() instead of adding to the current value:
UPDATE mindcore_sessions SET session_time = DATE_ADD(NOW(), INTERVAL 3 HOUR) WHERE session_id = '$sessionId';
There is no way in mysql to know if the same query was executed before.
The only possible solution would be to save the original value in an extra column, and use this column to check if the value was changed in the past.
If you want the value to be ahead 3 hours from now, you can simply do this:
DATE_ADD(NOW(), INTERVAL 3 HOUR)
So, I need to reset the expiration dates for a bunch of coupon codes in our database. Our expirations dates are field "to_date" and are displayed as the following: to_date = '2013-04-14'
I need to set the to_date as 28 days after the from_date. So basically, something like this:
UPDATE salesrule
SET name = 'New coupon code', to_date = 'from_date + 28 days'
I know this would work for a simple int value, but I'm not sure how to do this give that the data displays as an actual date. I have no control over how the date itself displays, that's a built in Magento functionality.
I'm a big noob when it comes to MySQL, but I've done some research and I've found the format function: FORMAT(Now(),'YYYY-MM-DD') I have a feeling this may be the key... can someone point me in the right direction it terms of formatting or writing this command correctly? Thank you!
UPDATE salesrule
SET name = 'New coupon code', to_date = DATE_ADD(from_date, INTERVAL 28 DAY);
More info about the DATE_ADD() function here:
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
to_date = DATE_ADD(from_date, INTERVAL 28 DAY)
Check this question out, it does what you want.
You can use the DATE_ADD() function:
... WHERE DATE(DATE_ADD(eventdate, INTERVAL -1 DAY)) = CURRENT_DATE
It can also be used in the SELECT statement:
SELECT DATE_ADD('2010-05-11', INTERVAL 1 DAY) AS Tomorrow;
I'm trying to get all the records just for today but having trouble. Another thing I'm unsure of how to factor in is that my server time is two hours ahead of my local time so I'll need to figure out the offset. I'm off setting the time when I'm inserting the data just not sure how to do it on retrieval of if I'll need to.
mysql_query("SELECT `* FROM table WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= `date` AND `alert_status` ='0'") or die(mysql_error());
I think part of the problem is the <= I tried just using = and == but neither worked.
What exactly is the problem you're running into? What do you mean when you say that it doesn't work?
I see a syntax error (the backtick right before the asterisk), but I'm not sure what your issue is. That DATE_SUB call looks reasonable to me.
Edit: try something like this:
WHERE `date` BETWEEN DATE_SUB(CURDATE(),INTERVAL 1 DAY) AND CURDATE()
...which is just a fancy way of doing this:
WHERE `date` >= DATE_SUB(CURDATE(),INTERVAL 1 DAY) AND `date` <= CURDATE()