UPDATE reservation SET flag = "1" WHERE ipAddress = (SELECT ipAddress FROM reservation WHERE endDate < CURRENT_TIMESTAMP);
Im trying to use this query for changing flag column of those entries in reservation table whose date has expired. The flag column is by default 0. so im trying to change expired once to 1 for my identification.
Im getting the following error.
ERROR 1093 (HY000): You can't specify target table 'reservation' for updatein FROM clause
Can someone suggest a solution to this problem..
if you're using the same table in the subselect, you can probably omit the subselect altogether.
why dont you use the following instead
UPDATE reservation
SET flag = "1"
WHERE endDate < CURRENT_TIMESTAMP
You do not need the inner SELECT. You just need to update the flag status based on the endDate
UPDATE reservation
SET flag = "1"
WHERE endDate < CURRENT_TIMESTAMP
Related
Say I have a mySQL table of a few columns:
id, name, job, jobUpdatedAt
Whenever specifically the job column changes, I want mySQL to automatically update the timestamp of jobUpdatedAt. (so if only name changes, it does not update)
Is such thing possible?
Thanks
CREATE TRIGGER trigger_name
BEFORE UPDATE
ON table_name
FOR EACH ROW
SET NEW.jobUpdatedAt = CASE WHEN OLD.job = NEW.job
THEN OLD.jobUpdatedAt
ELSE CURRENT_TIMESTAMP
END;
jobUpdatedAt will be renewed only when job is updated by fact.
jobUpdatedAt will be saved if the query should try to update it explicitly.
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=1b48bce653eb9a1d778e406fa5af894a
I have a Denormalized database where I have the total points of other table into the driver table. I want to update the total points of the table driver when I update the table where the points are.
Something like this:
CREATE TRIGGER sanciones_trigger BEFORE UPDATE ON points
FOR EACH ROW
UPDATE drivers,
( SELECT pID,SUM(numpoints) AS total_points
FROM drivers
INNER JOIN points ON points.driverID = drivers.pID
GROUP BY drivers.pID
) sum
SET drivers.total_points= sum.total_points
WHERE drivers.pID = sum.pID;
But I cant Update inside the trigger. I also tried with a procedure, but I'm don't really know how to do it.
How can I resolve this?
If the 'numpoints' is available on 'points' table you can just add the delta into 'drivers' on each update through a trigger. However this won't work if you are changing the 'driverID' as well. Please try the following.
here 'NEW' keyword gives you the new value that you are changing to and 'OLD' keyword gives you the value that exists on the table.
CREATE TRIGGER sanciones_trigger BEFORE UPDATE ON points
FOR EACH ROW
UPDATE drivers
SET total_points= drivers.total_points + (NEW.numpoints - OLD.numpoints)
WHERE pID = NEW.driverID;
I'm trying to execute a statment in MySql to update a column in a table when the expiry date for one of the other columns surpasses the current date, this is then compared against something to make sure that there are no active people for it
but i keep getting this error , i can not see anything wrong with my syntax so im not sure what it is
Error 1305 PROCEDURE does not exist
UPDATE job j SET archived = 1 WHERE(SELECT count(*) FROM job_applied_candidates jac WHERE jac.jobID = j.id) = 0 AND enddate < now();
Add a space between WHERE an (Select...
Also check triggers on jobs table, since they could use a procedure that does not exist.
I need to keep track of the time a row was inserted into the database, and the time it was last modified.
I tried to create two separate columns, and use CURRENT_TIMESTAMP:
create table def (
id int,
creation timestamp
default CURRENT_TIMESTAMP,
modification timestamp
on update CURRENT_TIMESTAMP
);
However, this produced an error:
ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
What is the best way to do this?
I'm thinking stored procedure, but looking for a standard solution. I'm also concerned with access privileges -- as few programs/things should be able to touch the timestamps as possible.
Although I would prefer MySQL answers, solutions for other RDBMS's are also appreciated!
Ya this is a lame limitation on MySQL. If you are going through an application you can add a time() call for the created_at column, and let the updated_at column use the CURRENT_TIMESTAMP.
$sql = "INSERT INTO my_table SET name = 'Mike', created_at = " . time();
I'd opt to do this on the created_at column as it probably won't be touched as often as the updated_at column.
-- Edit --
Better yet, use MySQL's built in now() function. This way you only need to be concerned with the timezone of the mysql server, and not the timezones of the app server AND the mysql server.
$sql = "INSERT INTO my_table SET name = 'Mike', created_at = NOW()";
You can use a trigger. The application can also set the value, but if do, it will be overwritten by the database.
delimiter //
CREATE TRIGGER def_bef_update BEFORE UPDATE ON def FOR EACH ROW BEGIN
SET NEW.modification = CURRENT_TIMESTAMP;
END//
delimiter ;
You can also use it to check the data and update your modification date only if has important changes.
I have a table with a DEC field named product_price and I wanted to add a field called price_updated_date. Is there a way to set the table to automatically insert the current time-stamp whenever the product_price field has been updated?
If not, is there a way to set it to insert the current time-stamp any time the entry is updated at all?
update:
It seems like using a trigger is the best option here. I am new to triggers and having some trouble creating it. Here is my code:
CREATE TRIGGER price_update
AFTER UPDATE ON cart_product
FOR EACH ROW
IF(OLD.product_price != NEW.product_price)
THEN
UPDATE cart_product
SET price_updated_date = CURDATE()
WHERE product_id = NEW.product_id
This is giving me this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 8
Why not set properties for that field as:
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
yup, create trigger after update on your table
CREATE TRIGGER price_update AFTER UPDATE on _table_ FOR EACH ROW UPDATE _table_ SET price_updated_date = CURTIME() where id=NEW.id
I note you're using MySQL
The default behaviour of a MySQL timestamp field is to default to NOW() on insert AND on update
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
I know this isn't dependant on which column has been updated as you require, but may be of some use to you or someone else browsing this question.
Its a 5-second job - just add a timestamp field & hey presto
Anytime you run an update statement, make sure you do something like this:
UPDATE table SET `product_price` = 'whatever', price_updated_date = NOW()
This will always insert the current date/time when you change the row.
Use a database trigger:
http://dev.mysql.com/doc/refman/5.0/en/triggers.html