I'm trying to update a column from a MySQL table so that it changes to current timestamp when the new date from another column is greater than the existing date from that column, and stays the same otherwise.
For example, let's say that I have a table with two columns, enddate and enddate_modif. If the date in enddate in the updating record is greater than the date in enddate in the existing table, I want enddate_modif to change to current timestamp. Otherwise, it keeps the same value.
How can I do it in MySQL?
Thank you in advance.
An after update trigger should do the trick.
Try:
CREATE TRIGGER `enddate_modif_trigger` BEFORE UPDATE ON `my_table`
FOR EACH ROW
SET NEW.enddate_modif = (SELECT CASE WHEN NEW.enddate > enddate_modif
THEN CURRENT_TIMESTAMP
ELSE NEW.enddate_modif
END
FROM my_table
);
I think you can use an after update trigger
something like this
if(New.enddate >Old.enddate ) then
update yourtable set enddate_modif=NOW() where Your_table_ID=NEW.Your_table_ID;
end iff;
Related
If I have a column in a table of type TIMESTAMP and has as default: CURRENT_TIMESTAMP does this column get updated to the current timestamp if I update the value
of any other column in the the same row?
It seems that it does not but I am not sure if this is what should happen.
I can not understand what this means (from MySQL documentation):
If the column is auto-updated, it is automatically updated to the
current timestamp when the value of any other column in the row is
changed from its current value. The column remains unchanged if all
other columns are set to their current values. To prevent the column
from updating when other columns change, explicitly set it to its
current value. To update the column even when other columns do not
change, explicitly set it to the value it should have]2
Give the command SHOW CREATE TABLE whatever
Then look at the table definition.
It probably has a line like this
logtime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
in it. DEFAULT CURRENT_TIMESTAMP means that any INSERT without an explicit time stamp setting uses the current time. Likewise, ON UPDATE CURRENT_TIMESTAMP means that any update without an explicit timestamp results in an update to the current timestamp value.
You can control this default behavior when creating your table.
Or, if the timestamp column wasn't created correctly in the first place, you can change it.
ALTER TABLE whatevertable
CHANGE whatevercolumn
whatevercolumn TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
This will cause both INSERT and UPDATE operations on the table automatically to update your timestamp column. If you want to update whatevertable without changing the timestamp, that is,
To prevent the column from updating when other columns change
then you need to issue this kind of update.
UPDATE whatevertable
SET something = 'newvalue',
whatevercolumn = whatevercolumn
WHERE someindex = 'indexvalue'
This works with TIMESTAMP and DATETIME columns. (Prior to MySQL version 5.6.5 it only worked with TIMESTAMPs) When you use TIMESTAMPs, time zones are accounted for: on a correctly configured server machine, those values are always stored in UTC and translated to local time upon retrieval.
I think you have to define the timestamp column like this
CREATE TABLE t1
(
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
See here
An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. An auto-updated column remains unchanged if all other columns are set to their current values.
To explain it let's imagine you have only one row:
-------------------------------
| price | updated_at |
-------------------------------
| 2 | 2018-02-26 16:16:17 |
-------------------------------
Now, if you run the following update column:
update my_table
set price = 2
it will not change the value of updated_at, since price value wasn't actually changed (it was already 2).
But if you have another row with price value other than 2, then the updated_at value of that row (with price <> 3) will be updated to CURRENT_TIMESTAMP.
Add a trigger in database:
DELIMITER //
CREATE TRIGGER update_user_password
BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF OLD.password <> NEW.password THEN
SET NEW.password_changed_on = NOW();
END IF;
END //
DELIMITER ;
The password changed time will update only when password column is changed.
Adding where to find UPDATE CURRENT_TIMESTAMP because for new people this is a confusion.
Most people will use phpmyadmin or something like it.
Default value you select CURRENT_TIMESTAMP
Attributes (a different drop down) you select UPDATE CURRENT_TIMESTAMP
Hello there I am trying to update an existing column and set the year column from 1/1/2019 to just 2019. I am getting a syntax error not sure how to do this. Thank You
UPDATE TABLE set year = YEAR(date_format(STR_TO_DATE(year,'%d/%m/%Y')))
WHERE id > 0;
I instead suggest that you don't store the year by itself, but rather create a new bona fide date column, and use STR_TO_DATE to populate it with your current data:
ALTER TABLE yourTable ADD COLUMN new_date DATETIME;
UPDATE yourTable
SET new_date = STR_TO_DATE(year, '%d/%m/%Y');
Afterwards, you may drop the previous text column year, assuming you don't need it anymore:
ALTER TABLE yourTable DROP COLUMN year;
Note that if you want to obtain the year from new_date, then you may easily do so, e.g.
SELECT new_date, YEAR(new_date) AS year
FROM yourTable;
This will Work For You Try this
UPDATE `Table` SET`Year`= STR_TO_DATE(year(CURDATE()),"%Y");
This will be only set the year but not the whole date
----------------------Note-------------------------
if you want to totally drop the month and the day change the datatype from date to year of your column in mysql if you want to know more about the Year datatype check out this link : https://dev.mysql.com/doc/refman/8.0/en/year.html
Okay I figured it out seems way more complicated than it should be. You need to convert the date to unix_time and the from_unixtime you need to select the year
UPDATE my_table
set year=YEAR(FROM_UNIXTIME(UNIX_TIMESTAMP(STR_TO_DATE(year, '%d/%m/%Y'))))
WHERE id > 0;
If I have a column in a table of type TIMESTAMP and has as default: CURRENT_TIMESTAMP does this column get updated to the current timestamp if I update the value
of any other column in the the same row?
It seems that it does not but I am not sure if this is what should happen.
I can not understand what this means (from MySQL documentation):
If the column is auto-updated, it is automatically updated to the
current timestamp when the value of any other column in the row is
changed from its current value. The column remains unchanged if all
other columns are set to their current values. To prevent the column
from updating when other columns change, explicitly set it to its
current value. To update the column even when other columns do not
change, explicitly set it to the value it should have]2
Give the command SHOW CREATE TABLE whatever
Then look at the table definition.
It probably has a line like this
logtime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
in it. DEFAULT CURRENT_TIMESTAMP means that any INSERT without an explicit time stamp setting uses the current time. Likewise, ON UPDATE CURRENT_TIMESTAMP means that any update without an explicit timestamp results in an update to the current timestamp value.
You can control this default behavior when creating your table.
Or, if the timestamp column wasn't created correctly in the first place, you can change it.
ALTER TABLE whatevertable
CHANGE whatevercolumn
whatevercolumn TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
This will cause both INSERT and UPDATE operations on the table automatically to update your timestamp column. If you want to update whatevertable without changing the timestamp, that is,
To prevent the column from updating when other columns change
then you need to issue this kind of update.
UPDATE whatevertable
SET something = 'newvalue',
whatevercolumn = whatevercolumn
WHERE someindex = 'indexvalue'
This works with TIMESTAMP and DATETIME columns. (Prior to MySQL version 5.6.5 it only worked with TIMESTAMPs) When you use TIMESTAMPs, time zones are accounted for: on a correctly configured server machine, those values are always stored in UTC and translated to local time upon retrieval.
I think you have to define the timestamp column like this
CREATE TABLE t1
(
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
See here
An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. An auto-updated column remains unchanged if all other columns are set to their current values.
To explain it let's imagine you have only one row:
-------------------------------
| price | updated_at |
-------------------------------
| 2 | 2018-02-26 16:16:17 |
-------------------------------
Now, if you run the following update column:
update my_table
set price = 2
it will not change the value of updated_at, since price value wasn't actually changed (it was already 2).
But if you have another row with price value other than 2, then the updated_at value of that row (with price <> 3) will be updated to CURRENT_TIMESTAMP.
Add a trigger in database:
DELIMITER //
CREATE TRIGGER update_user_password
BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF OLD.password <> NEW.password THEN
SET NEW.password_changed_on = NOW();
END IF;
END //
DELIMITER ;
The password changed time will update only when password column is changed.
Adding where to find UPDATE CURRENT_TIMESTAMP because for new people this is a confusion.
Most people will use phpmyadmin or something like it.
Default value you select CURRENT_TIMESTAMP
Attributes (a different drop down) you select UPDATE CURRENT_TIMESTAMP
need an advice, how to auto-store datetime value for my historyActivity table in select insert mysql query. This is an example:
INSERT INTO history_sequence(CODE, LAST_MOUNTH, LAST_VALUE) SELECT CODE, MOUNTH, VALUE FROM seq WHERE CODE = CODEVALUE
i just want to add datetime to see time when the data inserted. Need help please
You can do this in the MySQL table definition:
ALTER TABLE history_sequence ADD inserted TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
When records are inserted into the table table, the inserted column gets automatically populated with the current timestamp.
Is it possible to update zero columns in a MySQL update query?
I'd like to utilise the automatic updating of the timestamp column when this particular row is selected. Does anyone have any clue?
So you want your update query to only update the timestamp column? Just update it yourself instead of relying on the automatic update:
UPDATE mytable SET tscolumn = NOW() WHERE ...
Just try something like :
UPDATE myTable SET someField = someField WHERE id = myId
So the data values won't move, and the automatic timestamp will be updated.
As said by GaryG, you may also update the timestamp directly.