updating a field on update of other field in same row - mysql

I have a Table like this:
user_level
uid | level_order | current_level
---------------------------------
1 | 1,2,3 | 1
2 | 4,5,6 | 4
3 | 7,8,9 | 7
now, if I update level_order field for a particular user, i want to update current_level using trigger or procedure.
For Example if I run this query :
update user_level set level_order = '21,22,23' where uid=1;
then table should update like this:
uid | level_order | current_level
---------------------------------
1 | 21,22,23 | 21
2 | 4,5,6 | 4
3 | 7,8,9 | 7
Is it possible using trigger or procedure.
I am using MYSQL.

I think you dont need trigger.You can update it directly like this.
update user_level
set level_order = '21,22,23',
current_level=SUBSTRING_INDEX('21,22,23', ',', 1)
where uid=1;
Sql Fiddle Demo

Try following code
create trigger AutoUpdateCurrentLevelFromUserLevel
after update
on UserLevel
for each row
set CurrentLevel="{your value here}"

You can use trigger to achieve the same. Trigger are very useful in scenario like the one you mentioned above for implementing constraints. Use a update trigger for the same. Also you can embed the update to the new row in your query itself.

Related

Create Trigger : update table column by inserting data into another table rows

Please Help me to create triggers,
I have 2 tables of
data
+---------+------------+-------+-------+
| id | info_id | rate | qty | total |
+----+---------+------------+----------+
| 80 | 10 | 150 | 5 | 750 |
+----+---------+-------+-------+-------+
| 81 | 10 | 50 | 5 | 250 |
+--------------+-------+-------+-------+
info
+---------+---------------+------------+
| id | name | gtotal | dated |
+-----+--------+----------+------------+
| 10 | Hari | NULL | 2021-05-15 |
+---------+------------+---------------+
I want to create a trigger through phpmyadmin, as soon as data will be inserted, then info.gtotal will be updated by adding data.total from matched info_id from table name- data.
what will be the trigger if update on data happens too. I just want to create both triggers.
I am new with such, please help me. Any help is accepted.
You need to create two triggers one for insert and another for update.
CREATE TRIGGER `insert_trigger`
AFTER INSERT ON `data`
FOR EACH ROW
UPDATE info
SET gtotal = gtotal + new.total
WHERE id = new.info_id;
Here new.info_id will have the info_id value of newly insert record
CREATE TRIGGER `update_trigger`
AFTER UPDATE ON `data`
FOR EACH ROW
UPDATE info
SET gtotal = (gtotal - old.total) + new.total
WHERE id = new.info_id;
Here old.total will have the total value of record before updating. new.total is the total value after updation.
Since you haven't conveyed what to do after update I have added the logic of subtracting the total from old total and added the new total value. Change it as per your requirement.
You can also create these triggers using GUI in phpmyadmin. You have to select the triggers in the menu bar and add the trigger definition.

Mysql Trigger - Update table when insert another table

I have three tables:
Import Table:
import_id | import_date
-------------------------
1 | 2019/07/29
2 | 2019/07/28
ImportItem Table:
import_id | product_id | quantity
-------------------------------------
1 | 1 | 50
1 | 2 | 60
1 | 3 | 20
Product Table:
product_id | quantity
---------------------------
1 | 10
2 | 5
3 | 15
I want to create a trigger update quantity in product table when insert ImportItem table.
Any help much appreciated! Thanks very much!
If you create a trigger in your table import_item that will update your table product after each import_item INSERT you can do this :
CREATE TRIGGER trigger_name
AFTER INSERT
ON import_item FOR EACH ROW
BEGIN
UPDATE product SET quantity = NEW.quantity WHERE product_id = NEW.product_id;
END
Here is some documentation about the trigger key words NEW and OLD : documentation
When you are in a BEFORE/AFTER INSERT trigger, you can use NEW.field to get the value of the new field you just inserted.
Now if you are in a BEFORE/AFTER UPDATE trigger, you can use NEW.field to get the value of the field AFTER the update and OLD.value to get the value of the field BEFORE the update.
And the last, if you are in a BEFORE/AFTER DELETE trigger , you can use OLD.field to get the value fo the field before the delete.
Is it what you are looking for ?

Extracting data from column and put them in another column

I'm having a problem with building a query.
What I have is this in THE db
--------------------------------------
Id |name | profilenr | nr
--------------------------------------
1 | Harry| admin-124 | NULL
2 | Barry| admin-267 | NULL
6 | gerry| user-689 | NULL
9 | larry| user-435 | NULL
What I want to do is:
Getting only the numbers from the profilenr column and put them in the nr column of each profile that starts whit admin- .
In this example only for harry 124 in colum nr
And for Barry only 267 in colum nr.
I know this is possible but don't know how to build the query for this.
May be more like this:
update my_table update
set nr = substr(profilenr, locate('-', profilenr)+1, 3)
where profilenr like 'admin-%';
You can use a substr and locate
update my_table
set nr = substr(profilenr, locate('-',profilenr)+1, 3);

How to apply automated query after each insert with condition?

If I have a table like that:
id | name | points
------------------
1 | Hala | 50
2 | Asa | 60
3 | Hala | 1000
How can I apply automated query that applied after each insert but with condition
like this query but only to apply on the latest item after it is inserted
update points set points = points / 10 where name = 'Hala';
Try to create a trigger like this
CREATE TRIGGER MyTrigger AFTER INSERT ON MyTable
FOR EACH ROW BEGIN
SET NEW.points = NEW.points/10;
END

MySQL Insert or Update (Where 2 columns match)

I have a table structure like below (like dummy data below).
RecordId | UserId | TestId | Score
----------------------------------
1 | 1 | 4 | 98
2 | 1 | 5 | 92
3 | 1 | 6 | 91
4 | 2 | 4 | 99
5 | 2 | 5 | 07
6 | 2 | 6 | 08
I want to update the above but I don't have the RecordId handy.
So lets say UserId 2 on TestId 5 got a Score 55.
We don't currently know wether a record even exists for UserId 2 on TestId 5 so it needs to add the data if it doesn't already exist. If The UserId and TestId do already exist I need to update them.
I don't 'think' i want to use any of the replace queries as I have read that these delete the old record and a create new one, which would have a new Id.
I 'think' it needs to be update with a on duplicate update but i cannot get this to work?
Any help would be much appreciated.
Ok so here is the answer.
Turns out my query was fine but i needed to create a unique key
Here is the prepared SQL statement I used.
INSERT INTO test (UserId, TestId, Score) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE UserId=? TestId=? Score=?;
To make this update when unique combination of UserId & TestId I used the follow SQL query.
ALTER TABLE test ADD UNIQUE KEY `UserTests` (`UserId`, `TestId`);
I hope this & my explanation helps someone out.