Update Table A with content from Table B - mysql

Im trying to make a trigger in mySQL that will update a colum with data from another table, when i do a insert.
I got table: rtsEP_groups
With: id, name
and table: rtsEP_users
with: id, groups (and alot of others)
And i want to update "groups" in rtsEP_users, with the "id" from rtsEP_groups where "name" is "Student", when there is a new insert in rtsEP_users.
I was working on this trigger, but not sure if its right, and how to finish it.
How to update the right colum.
CREATE TRIGGER defaultGroup
AFTER INSERT ON rtsEP_users
BEGIN
DECLARE _group_id INTEGER;
SELECT id INTO _group_id FROM rtsEP_groups WHERE name = "Student"
UPDATE rtsEP_users SET groups = _group_id WHERE

Use BEFORE INSERT instead of AFTER, so New is represent of inserted row that you can update.
Try this
CREATE TRIGGER defaultGroup BEFORE INSERT ON rtsEP_users
FOR EACH ROW
SET New.groups = (SELECT id FROM rtsEP_groups WHERE name = "Student");

Related

How can I create a trigger in phpmyadmin, that after insert will update a field in anther table?

I have a table called userlikes that has columns give_like_id, receive_like_id.
I also have a users table with count_likes and id columns. Every time a new record is inserted into userlikes table, I want to decrease by -1 in countlikes table.
userlikes: give_like_id, receive_like_id
users: id, count_likes
I tried this code, but it's not working.
CREATE TRIGGER `update_count_likes`
AFTER INSERT
ON `user_likes`
FOR EACH ROW
UPDATE users
SET users.likes_count = users.likes_count - 1
WHERE users.id = user_likes.give_like_id
hey, the answer for it:
CREATE TRIGGER `update_count_likes` AFTER INSERT ON `user_likes`
FOR EACH ROW UPDATE users
SET users.likes_count
= users.likes_count -1
WHERE users.id=NEW.give_like_id

MYSQL: Trigger don't Update the last row in table with the value of another table

I have created the trigger to Update the "request" table whenever the value is inserted into other table - "user". The column that must be updated is "UserID" with the value of "ID":
CREATE DEFINER=`root`#`localhost` TRIGGER `database`.`user_AFTER_INSERT`
AFTER INSERT ON `user`
FOR EACH ROW
BEGIN
UPDATE request SET UserID = new.ID
where id = (select * from (select max(id) from request) as t);
END
But, the code is not updating the last row, it is updating the row before the last one, please see the screenshots:
user table:
request table:
Any help will be really appreciated.

Set max value as default value

I'm trying to store the order of the products in a database, and I want every new product added to be at the last position. Basically, I want the "rank" field to be set to MAX + 1, and MAX should be the highest value between the entries having the same "type".
I tried to do with a trigger, but since MySQL won't let me do that since the trigger would be executed on the same table :
**#1442 - Can't update table 'products' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. **
Here is what my trigger was like :
CREATE TRIGGER `productInsert`
AFTER INSERT ON `products`
FOR EACH ROW
BEGIN
UPDATE products
SET rank = (SELECT MAX(rank)
from products
where type_id = NEW.type_id)
where id = NEW.id
Is there any other way to do that?
Thanks
You want a before insert trigger:
CREATE TRIGGER `productInsert`
BEFORE INSERT ON `products`
FOR EACH ROW
BEGIN
set NEW.rank = (select max(rank) + 1
from products p
where p.type_id = NEW.type_id
);
END

Make an sql value equal to another value from another table on insert

basically I have these both tables.
What I want to do is upon INSERTION of content in the second table (table 2) I want its value at NULL to be filled with the id from table 1 in which the pairs formed by part1/part2 from table 1 and part1/part2 from table 2 remain the same (please notice they can exchange between them). What is the best way to do this?
What you can do is create a trigger on Insert like below
CREATE TRIGGER grabIdFromTable1
BEFORE INSERT ON table2
FOR EACH ROW
BEGIN
SET NEW.id = (SELECT id FROM table1 WHERE part1 = NEW.part1
AND part2 = NEW.part2
);
END//
see this sqlFiddle

MySQL - How can add a new column synchronized with another table's sum of multiple columns

I have to table named 'stuff' and 'given'. I wanted to add one more columnt to 'stuff' and after ALTER TABLE i used UPDATE. However this makes difference for only that moment. I mean, m new column will give the amount of stock after each given amount. 'stuff' has 'totalAmount' and given has 'amount'.
I used:
ALTER TABLE stuff ADD stock MEDIUMUNINT UNSIGNED;
UPDATE stuff SET stuff = totalAmount - (SELECT SUM(given.amount) FROM given WHERE id = given.productId);
This works for only that UPDATE. How can i make these two table so synchronized after each given amount, the stock will be affected either?
Thanks in advance :)
Create an trigger on INSERT for the table "given" which will update the "stuff" in table "stuff"
CREATE TRIGGER [TRIGGER_ALTER_STUFF] ON [dbo].[given]
FOR INSERT, UPDATE
AS
BEGIN
UPDATE stuff SET stuff = totalAmount - (SELECT SUM(given.amount) FROM given WHERE id = given.productId);
END