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;
Related
I've added a new column to the ls_customer table that goes by response_category. I then set an update code to it.
ADD statement:
ALTER TABLE ls_customer
ADD response_category VARCHAR(255)
UPDATE Statement:
UPDATE ls_customer SET response_category = 'Hot Lead' WHERE flag = 3
UPDATE ls_customer SET response_category = 'Warm Lead' WHERE flag = 2
How do I set the above "UPDATE" statement to auto update whenever the flags change in our internal CRM tool?
I then use a SELECT statement to present this data. The problem is this "SELECT" statement is linked to my Tableau analytics. Everytime I refresh Tableau, if there's any changes in the flag, the response category doesn't get updated in the final SELECT statement untill I manually open MySQL and run the above "UPDATE" query again.
The SELECT statement is as follows:
SELECT
cell_phone,full_name,company_name,DATE(date_join),
response_type,response_category,ls_customer.tags AS lead_type,ls_lead_stats_raw_data.last_update,date_join,source
FROM ls_lead_stats_raw_data
INNER JOIN ls_customer
ON ls_lead_stats_raw_data.client_id = ls_customer.customer_id
INNER JOIN ls_company
ON ls_company.company_id = ls_customer.company_id
How do I automate the UPDATE statement to set response_category to 'Hot Lead', 'Warm Lead' everytime I change the flags in our CRM?
Despite mysql version number, there are many different ways you can solve this issue, such as:
creating a before update trigger on your table updating response_category column based on flag column content. But, maybe your CRM already use triggers and will not allow you to add any code to them
creating an database event executed every minute and updating the response_category column. this solution is not the most effective as it will execute the event, even nothing has changed in the flag column.
you can create a reference table with the flag and the response_category columns. It's content would be :
flag
response_category
2
warm lead
3
hot lead
You add this new table with a join on ref_table.flag = ls_customer.flag to your select (replace response_category by ref_table.response_category). But this solution is so obvious, that I think there is a good reason you didn't use it.
So finally I would recommend to use a generated column instead of a regular column for response_category:
ALTER TABLE ls_customer
ADD COLUMN response_category VARCHAR(255) GENERATED ALWAYS
AS (CASE flag WHEN 2 THEN 'Hot lead' WHEN 3 THEN 'Warm lead' END)
STORED ;
Using STORED option will allow you to add an index on this column in case you need to improve your SELECT performance.
Hope this will help.
I am trying to get the sum of table Product_sizes 'stock' column and put it in a variable. After that, I intend to update the Products table 'stock' column. But when I try to run the query, it shows a syntax error.
Here's the 'Products' table
Here's the 'Product_sizes' table
And here's my SQL query
CREATE TRIGGER product_sizes_stock_sum BEFORE UPDATE on product_sizes
FOR EACH ROW
BEGIN
SELECT SUM(stock) INTO #stocksum FROM product_sizes WHERE product_id = OLD.product_id;
UPDATE products SET stock = #stocksum where id = OLD.product_id;
END
Here's the error message
I am following the Laravel convention of naming tables.
I'm really confused right now. Thanks for the help in advance
I want to update a column in MySQL only if the row is updated. (meaning all the other values trigger mysql to update the row.)
It's a concatenated text-field, so automatic calculation using ON UPDATE in the table definition won't work.
A trigger won't work either since the value is not fixed.
If there is ON UPDATE in table definition or triggers, there must be a way to determine that in some expression, right?
(Of course, I could create/update a trigger every time after the update or do a second update based on automatic update timestamp (which I then need to select first...) but that's both not very effective nor elegant.)
What I'd love to do would be something like this:
UPDATE tbl
SET x = 1, y = 2
ON UPDATE ( z = CONCAT_WS(', ',z,'blah') );
I have a database with 2 tables. One tables contains data regarding references (job refs) the other contains votes. In order to get to the vote stage, you need 5 references. In another database, i must change a group_id once they have 5 refs.
I was trying to use something like below:
CREATE TRIGGER update_group AFTER UPDATE ON refs
FOR EACH ROW
BEGIN
IF (SELECT recruit_id from refs where count(refs) = 5) THEN
UPDATE users.members
SET group_id=10
WHERE member_id=recruit_id;
END;
Is something like this even feasible? if this doesn't work, my fallback is to use a bash script in a cron job that runs every 5 minutes, but a trigger just seems so much more efficient (and instantaneous)
You may use NEW. to refer respectively affected recruit_id after change, do a count and update the foreign table accordingly.
CREATE TRIGGER update_group AFTER UPDATE ON refs
FOR EACH ROW
BEGIN
SELECT #refs_no := count(*) from refs where recruit_id = NEW.recruit_id
IF (#refs_no = 5) THEN
UPDATE users.members
SET group_id=10
WHERE member_id=NEW.recruit_id;
END;
UPDATE items SET name = 'haha' WHERE id = '12'
I'm curious if update also inserts the values if the where condition fails. I've read on w3schools that update only updates existing data on the database but on my script it's automatically inserting rows with the data. I am wondering if it might be a bug in the script or that's just how UPDATE works on mysql.
No. If, in your example, there's no entry with id = 12 in the database, the query will return "no rows affected". An update will never create a new entry in MySQL.
EDIT: although update won't create a new entry, it may include default/automatic values set up in your database schema (current timestamp, for instance).
NO. Update does not insert a value if the value doesn't exist in table. Please check if the script checks if the status of the update and makes another call to DB to insert the data.
Your SQL should do the following -
Update all records in the items table that have an id of 12 by setting their name to 'haha'
Update won't insert records if they don't exist, it will only update existing records in the table.
Short answer: No.
Long Answer: If your column doesn't exist you will get an error. If your where condition column doesn't exist you get error too. If your where condition value doesn't exist, it do nothing.
I use a temp table to review the data update conditions, you can refer
UPDATE table1
SET
column1 = 'things'
WHERE
IDcolumn = 'id' AND
(NOT EXISTS (SELECT * FROM (SELECT * FROM table1) AS temp WHERE temp.column1 = N'things'))