What would be the proper way to do this? tried many things and searched everywhere online
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 DAYS
DO
BEGIN
SELECT CASE WHEN funded IS NULL OR funded = ''
THEN INSERT INTO mytable1 (`id`, `to`) VALUES (NULL, 'admin')
ELSE
FROM mytable2 WHERE id='$id';
END
Try like this
DECLARE xFounded varchar(10);
SELECT funded into xFounded FROM mytable2 WHERE id='$id';
if xFounded is null or xFounded ='' then
INSERT INTO mytable1 (`id`, `to`) VALUES (NULL, 'admin');
ELSE
// Do Else Part Here
end if;
This is probably the shorest way.
insert into mytable1 (`id`, `to`) VALUES (NULL, 'admin') on duplicate key update id=LAST_INSERT_ID(id), to=to;
select LAST_INSERT_ID();
Related
Trigger definition:
BEGIN
INSERT INTO `user` (`ID`, `Username`, `Password`, `Online`) VALUES
(NEW.ID, "store_"NEW.ID , 'blablabla', 0);
END
In this trigger I have to insert as username something like store_(id of the new store).
Is it possible to do this and if yes how?
There are 2 ways to concat/append string in SQL
a. Use + operator to join the string
b. Use Concat function.
By using + operator the query would be like below.
BEGIN
INSERT INTO `user` (`ID`, `Username`, `Password`, `Online`) VALUES
(NEW.ID, "store_" + NEW.ID , 'blablabla', 0);
END
By using Concat() function the query would be like below.
BEGIN
INSERT INTO `user` (`ID`, `Username`, `Password`, `Online`) VALUES
(NEW.ID, Concat("store_",NEW.ID) , 'blablabla', 0);
END
Recommended option is use 'Concat' function as syntax is easier to follow, and the code looks cleaner
Use concat function :
BEGIN
INSERT INTO user (ID, Username, Password, Online)
VALUES
(NEW.ID, CONCAT('store_',NEW.ID), 'blablabla', 0);
END
I want both statements to be executed in my trigger. I can't get the following to work:
IF (flag = 'D') THEN
BEGIN
INSERT INTO logs (id, author_id, action_done, description, old_value, new_value, create_date)
VALUES (null, (SELECT id FROM gallery WHERE flag = 'D'), 'Delete', 'Gallery', (SELECT filename FROM gallery WHERE flag = 'D'), '', NOW());
DELETE FROM gallery WHERE flag = 'D';
END
END IF
I assume id field in logs table is a primary key and auto generated by system.
Try this:
IF (flag = 'D') THEN
BEGIN
INSERT INTO logs (author_id, action_done, description, old_value, new_value, create_date)
select id, 'Delete', 'Gallery', filename, '', NOW() from gallery where flag='D';
DELETE FROM gallery WHERE flag = 'D';
END
END IF
Your id should auto increment and thus should not be one of the values.
Presumably, your trigger is on the table gallery. If so, your trigger should be a "before" or "after" trigger and you can do:
IF (flag = 'D') THEN
BEGIN
INSERT INTO logs (author_id, action_done, description, old_value, new_value, create_date)
VALUES (old.id, 'Delete', 'Gallery', old.filename, '', NOW());
END;
END IF
There is no need to repeat the DELETE.
I'm building a data versioning system, and I need to increment a version number each time a new row is added to the version table, but it increments once and then stops:
DELIMITER |
CREATE TRIGGER trigger2 AFTER UPDATE ON something
FOR EACH ROW
BEGIN
IF NEW.updated_at <> OLD.updated_at THEN
INSERT INTO versions_something (
`id`,
`some_id`,
`version`,
`title`,
`description`,
`created_at`,
`updated_at`
) VALUES (
null,
NEW.id,
1,
NEW.title,
NEW.description,
NOW(),
NOW()
);
END IF;
UPDATE
versions_something
SET
version = (SELECT MAX(version)) + 1
WHERE versions_something.id = LAST_INSERT_ID();
END;
|
DELIMITER ;
I've tried putting the UPDATE into a separate trigger (AFTER INSERT ON versions_something ...), but MySQL complains that it's clashing with the trigger before it.
I've tried the UPDATE on its own, using the last ID in the table and it works each time, so I have no idea what's happening.
I am trying to group users by their email address.
REGEXP is not working. Help
BEGIN
DECLARE group_id tinyint(4);
SET #user_email = ( SELECT email FROM `users` WHERE id = NEW.id );
IF (SELECT #user_email REGEXP '\.com$') THEN SET group_id = 17;
ELSE SET group_id = 18;
END IF;
INSERT INTO `user_usergroup_map` (`user_id`, `group_id`) VALUES (NEW.id, group_id);
END
--------- New -------------------
I did some more testing.
This worked...
BEGIN
INSERT INTO `user_usergroup_map` (`user_id`, `group_id`) VALUES( 3, 18 );
END
But this didn't work
BEGIN
INSERT INTO `user_usergroup_map` (`user_id`, `group_id`) VALUES(
NEW.id, 18);
END
Rather than try to figure out what is not working, let me suggest this simplification:
BEGIN
INSERT INTO `user_usergroup_map` (`user_id`, `group_id`)
SELECT NEW.id,
IF( email REGEXP '[.]com$', 17, 18 )
FROM users
WHERE id = NEW.id;
END
One difference: Your version will always insert a row; mine won't if id is missing from users.
How would I end an if statement upon being satisfied but then execute the else if otherwise in a stored procedure?
I have tried putting an end if after the if statement:
IF EXISTS (SELECT * FROM `db`.`tbl` WHERE email = "email#domain.com") END IF;
ELSE INSERT INTO `db`.`tbl` (email) VALUES ("email#domain.com"); END IF;
But this didn't work.
Why not use an ignore insert?
INSERT IGNORE INTO `db`.`tbl` SET `email`= 'email#domain.com';
You would have to create a unique index on email.
Check here for some reference.
If this is for inserting if the row does not already exists, you shoul use this:
INSERT INTO `table` (value1, value2)
SELECT 'stuff for value1', 'stuff for value2' FROM `table`
WHERE NOT EXISTS (
SELECT * FROM `table`
WHERE value1='stuff for value1' AND value2='stuff for value2'
);