How to add seven days to DATE field - mysql

I would like to update each DATE field and add him 7 days. i check for DATE functions in php but i didn't found function that fit to MySQL syntax
my query is:
$query_sched = "UPDATE events SET events.event_date = DATE(events.event_date + INTERVAL 7)";
$res = mysqli_query($dblink, $query_sched) or die (mysqli_error($dblink));

You are missing a '+'
UPDATE events SET events.event_date = DATE(events.event_date + interval 7 day) WHERE events.event_id = '$event_id'
UPDATE events SET events.event_date = DATE_ADD(events.event_date , interval 7 day) WHERE events.event_id = '$event_id'
Output:
update test set test.date = date(test.date + interval 7 day) where test.id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Related

How to combine the below the update querys into one, Update multiple rows with multiple values for same column and the rest of data with other value

here i want to change the date for 9 rows
1.
UPDATE forum_topic_resume
SET _When_Updated = (now() -INTERVAL 6 day)
WHERE _id IN (96250, 69081, 69555)
UPDATE forum_topic_resume
SET _When_Updated = (now() -INTERVAL 8 day)
WHERE _id IN (70494, 68612, 69564, 69660, 72437, 80498)
The change the status
3.
UPDATE forum_topic_resume
SET _Status = 1224
WHERE _id IN (96250, 69081, 69555)
UPDATE forum_topic_resume
SET _Status_Is = 1228
WHERE _id IN (70494, 68612, 69564)
UPDATE forum_topic_resume
SET _Status_Is = 1229
WHERE _id IN (69660, 72437, 80498)
There are about 52 more Ids for whom I was to set the status to a different value which would look like below.
6.
UPDATE forum_topic_resume
SET _Status_Is = 1250
WHERE _id IN (for the rest of the ids)
One way is using multiple case conditions:
UPDATE forum_topic_resume
SET _When_Updated = CASE
WHEN _id IN (96250, 69081, 69555) THEN (now() -INTERVAL 6 day)
WHEN _id IN (70494, 68612, 69564, 69660, 72437, 80498) THEN (now() -INTERVAL 8 day)
other id conditions .............
END,
_Status = CASE
WHEN _id IN (96250, 69081, 69555) THEN 1224
WHEN _id IN (70494, 68612, 69564) 1228
......
END,
_Status_Is = CASE
WHEN _id IN (96250, 69081, 69555) THEN 1224
WHEN _id IN (70494, 68612, 69564) 1228
......
END;
Use multiple-table UPDATE.
UPDATE forum_topic_resume
LEFT JOIN (
SELECT 96250 AS _id,
now() - INTERVAL 6 day AS _When_Updated,
1224 AS _Status
UNION ALL
SELECT 70494, now() -INTERVAL 8 day, 1228
UNION ALL
...
) data_for_update USING (_id)
SET forum_topic_resume._When_Updated = COALESCE(data_for_update._When_Updated,
forum_topic_resume._When_Updated),
forum_topic_resume._Status= COALESCE(data_for_update._Status,
1250);
data_for_update contains all data needed for definite updating.
COALESCE provides storing _When_Updated value and setting definite _Status value for the rows with _id values which are not listed in data_for_update.
You may add some WHERE conditions when not all rows in forum_topic_resume not listed in data_for_update must be updated with new _Status value.
Of course the query text will be long, but the execution will be fast enough.

How to check does user is not active more than 1 minute

Hello I tried to update a row in table if somebody is offline more than 1 minute, and set LoggedIn then as 0, but my SQL query doesn't work, what I do wrong?
"UPDATE acc SET LoggedIn='0' WHERE LastTimeActive<(NOW(), INTERVAL 1 MINUTE)";
You are missing date_sub():
UPDATE acc
SET LoggedIn = '0'
WHERE LastTimeActive < date_sub(NOW(), INTERVAL 1 MINUTE);

Update Statement with Case in MYSQL

I have this issue and I have investigated more than five house but find nothing :( . I have table called support.
UPDATE support s SET s.Survey_Status = 0
CASE
WHEN s.Survey_Status = 0 Then 1
WHEN s.Survey_Status = 1 Then 2
End
Where last_response < ADDDATE(CURDATE(), INTERVAL 1 DAY)
and support_waiting ="n" ;
I need to update the support table and set the survey_status =1 except the fifth row in the table will be =2 . For example, if I have survey_ status from 1 to 10 = 1 then the fifth will =2 . any idea please ??
By the way, I am working with mysql Heidi .
Thanks in Advance
You can combine user variables and MOD():
UPDATE support, (SELECT #r:=0) init
SET Survey_Status = IF(MOD(#r:=#r+1,5), 1, 2)
WHERE last_response < CURRENT_DATE + INTERVAL 1 DAY
AND support_waiting = 'n'
ORDER BY ...

How can I update TIMESTAMP with Codeigniter and MySQL?

How can I update TIMESTAMP with Codeigniter and MySQL? I want to update with INTERVAL 1 MINUTE
I've tried the next code:
$data = array('is_active' => $state,
'timestamp_demo' => "DATE_ADD(NOW(), INTERVAL 1 MINUTE)");
$this->db->where('demo_session_id', 'web');
$this->db->update('demo_session', $data);
But not work. How can I do?
Try your query like this
$this->db->query("UPDATE demo_session
SET
is_active = 1,
timestamp_demo = DATE_ADD(NOW(), INTERVAL 1 MINUTE)
WHERE demo_session_id = 'web'");
You can do this with Active Records, which you're using:
$this->db->where('demo_session_id', 'web');
$this->db->set('is_active', $state);
$this->db->set('timestamp_demo', 'DATE_ADD(NOW(), INTERVAL 1 MINUTE)', FALSE);
$this->db->update('demo_session');
The third parameter in $this->db->set() prevents CodeIgniter from escaping the data so it'll use the MySQL function.
It'll end up something like this:
UPDATE 'demo_session' SET 'is_active' = $state, 'timestamp_demo' = DATE_ADD(NOW(), INTERVAL 1 MINUTE) WHERE 'demo_session_id' = 'web'

Whats wrong with my MYSQL QUERY?

Can someone please tell me what is wrong with my query.
I am trying to fetch data fro my table based on the column called weekend, if weekend is set to "0" show from Sunday 6pm until Friday 9pm, then if weekend is set to "1" Show from Friday 9pm until Sunday 6pm.
SELECT *
FROM closures
WHERE closures.weekend = 0
OR WEEKDAY(NOW()) < 4
AND closures.weekend = 1
OR WEEKDAY(NOW()) > 4
OR (WEEKDAY(NOW())=4 AND HOUR(NOW())>21)
OR (WEEKDAY(NOW())=6 AND HOUR(NOW())<18)
It helps to phrase the question properly. What you should have said is "between 9pm Friday and 6pm Sunday I want to show the rows where closures.weekend = 1, otherwise show those where closures.weekend = 0".
Hence what you need to do is generate a value of 1 or 0 depending on whether it's the weekend or not, and then SELECT those rows where weekend has that value, i.e:
SELECT *
FROM
closures
WHERE
weekend = IF(
(WEEKDAY(NOW()) = 4 AND HOUR(NOW()) >= 21)
OR (WEEKDAY(NOW()) = 5)
OR (WEEKDAY(NOW()) = 6 AND HOUR(NOW()) < 18)
, 1, 0)
Weekend cant be both, 0 and 1
SELECT *
FROM
closures
WHERE
closures.weekend = 0
OR
(
WEEKDAY(NOW()) < 4
AND closures.weekend = 1
)
OR WEEKDAY(NOW()) > 4
OR
(WEEKDAY(NOW())=4 AND HOUR(NOW())>21)
OR
(WEEKDAY(NOW())=6 AND HOUR(NOW())<18)
My first guess is that the
SELECT field1, field2, ...
is missing ?