Copy id after insert to another table - mysql

I have two tables, but I would like to set a trigger. When I insert a new user to my table users, I would like to copy that id to another table: results.
Table users:
| userID | name | email | password |
+--------+----------+-------------------+----------+
| 1 | Person A | mailA#gmail.com | 12345 |
+--------+----------+-------------------+----------+
| 2 | Person B | mailB#yahoo.com | 13579 |
+--------+----------+-------------------+----------+
| 3 | Person C | mailC#outlook.com | 24681 |
+--------+----------+-------------------+----------+
Table results:
| resultID | userID | TestA | TestB |
+----------+--------+-------+-------+
| 162 | 1 | 84 | 63 |
+----------+--------+-------+-------+
| 028 | 2 | NULL | 54 |
+----------+--------+-------+-------+
| 821 | 3 | 77 | 60 |
+----------+--------+-------+-------+
I would like to copy the userID from table users to userID in table results after insert.
I tried various options with triggers, but nothing fixed my problems.
One of them is:
CREATE TRIGGER T_TableA_I
on users
after insert
as
set nocount on
insert into results (userID)
select u.UserID
from
users u
inner join
results r
on
u.UserID = r.UserID
It may be that my structure is not in accordance with the guidelines, but this is a concept.

I am still not sure if I understand the question but here's a code snippet for conversation
drop table if exists us,res;
create table us (id int);
create table res (id int);
drop trigger if exists t;
delimiter $$
create trigger t after insert on us
for each row
begin
insert into res(id) values (new.id);
end $$
delimiter ;
insert into us values (1);
select * from us;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
select * from res;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

I have written the following code snippet, but I could not figure out where to get the test result!
CREATE TABLE IF NOT EXISTS users (
userID INT(6) UNSIGNED PRIMARY KEY,
name VARCHAR(255),
password VARCHAR(255),
email VARCHAR(255)
);
CREATE TABLE IF NOT EXISTS results (
resultID INT(6) UNSIGNED PRIMARY KEY,
userID VARCHAR(255),
TestA VARCHAR(255),
TestB VARCHAR(255)
);
/* trigger query */
CREATE TRIGGER new_user_added
AFTER INSERT ON users
FOR EACH ROW
INSERT INTO results values('162', NEW.userID, '84', '63');
/* insert query */
replace into users values('1', 'Person A', 'mailA#gmail.com', '12345');
select * from users;
select * from results
users
| userID | name | email | password |
+--------+----------+-------------------+----------+
| 1 | Person A | mailA#gmail.com | 12345 |
+--------+----------+-------------------+----------+
results
| resultID | userID | TestA | TestB |
+----------+--------+-------+-------+
| 162 | 1 | 84 | 63 |
+----------+--------+-------+-------+
Hope this could help.

Related

Using auto_increment id field value in other columns in the table

I am using mysql 8.0.30.
I have an orders table which is defined as follows:
create table orders (
id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
code varchar (64) NOT NULL,
ord_num varchar(512) NOT NULL );
After inserting a rows, I would like to have it as follows:
+----+------+-------+
| id | code | ord_num|
+----+------+-------+
| 1 | abc | abc-1 |
| 2 | def | def-2 |
+----+------+-------+
That is ord_num should have concatenation of code-id. I can achieve that by an update statement as follows:
update orders set ord_num = concat (code, '-', id ) where id = 2;
However, I would like to achieve that in the insert statemenet. For ex:
insert into orders (code, ord_num) values ("xyz", concat(code, '-', id));
This actually results in:
+----+------+-----------+
| id | code | ord_num |
+----+------+-----------+
| 1 | abc | abc-0 |
| 2 | def | def-2 |
| 3 | xyz | xyz-0 |
+----+------+-----------+
I know I can do this by starting a transaction, insert with some dummy value in ord_num, then updating it with the correct value before committing. I would prefer to do it in one insert statement, if possible?

How to add a set of different values in one column(MySQL)?

Do not judge strictly, but I can not figure it out in any way
My table:
CREATE table courses (id INT PRIMARY KEY AUTO_INCREMENT,
-> faculty VARCHAR(55) NULL,
-> number INT(10) NULL,
-> diff VARCHAR(10) NULL);
mysql> select * from courses;
Target. Inject values ('ez', 'mid', 'hard') into diff column.
For exampl, im trying this:
mysql> INSERT courses (diff) VALUES ('ez');
OR
mysql> UPDATE courses SET faculty = 'chem', number = 2, diff = 'mid';
Add rows with empty id(values NULL).
PLZ help me!
I want to get this result
+----+---------+--------+------+
| id | faculty | number | diff |
+----+---------+--------+------+
| 1 | bio | 1 | ez |
| 2 | chem | 2 | mid |
| 3 | math | 3 | hard |
| 4 | geo | 4 | mid |
| 5 | gum | 5 | ez |
+----+---------+--------+------+
You can use a case expression in an UPDATE statements:
UPDATE courses
SET diff=CASE
WHEN faculty in ('bio', 'gum') THEN 'ez'
WHEN faculty in ('chem', 'geo') THEN 'mid'
WHEN faculty = 'math' THEN 'hard'
END;

Inserting new data in a table

I have created a basic table for learning purposes.
CREATE TABLE friends (
id INT,
name TEXT,
birthday DATE
);
Added some data...
INSERT INTO friends (id,name,birthday)
VALUES (1,'Jo Monro','1948-05-30');
INSERT INTO friends (id,name,birthday)
VALUES (2, 'Lara Johnson','1970-03-03');
INSERT INTO friends (id,name,birthday)
VALUES (3,'Bob Parker', '1962-09-3');
And I realised that I forgot to include the email column.
I added the column...
ALTER TABLE friends
ADD COLUMN email;
..but how can I add now data to this new column only?
I have tried WHERE statements, rewriting the INSERT INTO statements with and without the other column names but nothing worked?
What am I missing here?
Thank you!
Insert the emails into a temporary table, then update the real table with that.
CREATE TABLE friends (
id INT auto_increment primary key,
name VARCHAR(100),
birthday DATE
);
INSERT INTO friends (name, birthday) VALUES
('Jo Monro','1948-05-30')
, ('Lara Johnson','1970-03-03')
, ('Bob Parker', '1962-09-3');
ALTER TABLE friends ADD COLUMN email VARCHAR(100);
select * from friends
id | name | birthday | email
-: | :----------- | :--------- | :----
1 | Jo Monro | 1948-05-30 | null
2 | Lara Johnson | 1970-03-03 | null
3 | Bob Parker | 1962-09-03 | null
--
-- temporary table for the emails
--
CREATE TEMPORARY TABLE tmpEmails (
name varchar(100) primary key,
email varchar(100)
);
--
-- fill the temp
--
insert into tmpEmails (name, email) values
('Jo Monro','jo.monro#unmail.net')
, ('Lara Johnson','lara.johnson#unmail.net')
, ('Bob Parker', 'UltimateLordOfDarkness#chuni.byo');
--
-- update the real table
--
update friends friend
join tmpEmails tmp
on friend.name = tmp.name
set friend.email = tmp.email
where friend.email is null;
select * from friends
id | name | birthday | email
-: | :----------- | :--------- | :-------------------------------
1 | Jo Monro | 1948-05-30 | jo.monro#unmail.net
2 | Lara Johnson | 1970-03-03 | lara.johnson#unmail.net
3 | Bob Parker | 1962-09-03 | UltimateLordOfDarkness#chuni.byo
db<>fiddle here

MYSQL Trigger UPDATE field when data is entered

I have a MYSQL table named procurement with the following columns...
| id | province | sample_result | image | status |
| 1 | prov_1 | 3 | url | Complete |
| 2 | prov_2 | 12 | NULL | |
| 3 | prov_3 | 45 | NULL | |
| 4 | prov_4 | | url | |
I would like to create a TRIGGER that updates the column "status" with "Complete" when both the sample_result and image columns are updated and no longer NULL. If either one is NULL then no update is required.
You can set NEW. values in a before trigger and since mysql copies OLD. values to NEW. values before it acquires NEW. values from the update then testing NEW. values is safe
drop table if exists t;
create table t
( id int, province varchar(10), sample_result int, image varchar(10), status varchar(10));
insert into t values
( 1 , 'prov_1' , 3 , 'url' , 'Complete'),
( 2 , 'prov_2' , 12 , NULL , null ),
( 3 , 'prov_3' , 45 , NULL , null ),
( 4 , 'prov_4' , null , 'url' , null );
drop trigger if exists t;
delimiter $$
create trigger t before update on t
for each row
begin
if new.sample_result is not null and new.image is not null then
set new.status = 'Complete';
end if ;
end $$
delimiter ;
update t set sample_result = 2 where id = 2;
update t set sample_result = 1 where id = 4;
update t set image = 'url' where id = 3;
select * from t;
+------+----------+---------------+-------+----------+
| id | province | sample_result | image | status |
+------+----------+---------------+-------+----------+
| 1 | prov_1 | 3 | url | Complete |
| 2 | prov_2 | 2 | NULL | NULL |
| 3 | prov_3 | 45 | url | Complete |
| 4 | prov_4 | 1 | url | Complete |
+------+----------+---------------+-------+----------+

Insert a row and select or insert foreign key via a subquery

For the last two days I am stuck with the following problem I want to insert data in the logintime table with one query. I am running MySQL 5.7.21
The problem I face however: there is a possibility that the player doesn't exists in the player table, so therefor I am looking for a solution that looks up the foreign key and if it doesn't exists create a new player in the player table and use the newly created player id in the logintime table.
Player table:
+----+-------+------+
| id | name | type |
+----+-------+------+
| 1 | Steve | 2 |
| 2 | Tim | 1 |
+----+-------+------+
Logintime table:
+---------------------+------------+----------+
| logintime | logouttime | playerid |
+---------------------+------------+----------+
| 2018-02-04 12:44:11 | NULL | 1 |
| 2018-02-03 10:55:32 | NULL | 2 |
| 2018-02-03 09:22:02 | NULL | 1 |
+---------------------+------------+----------+
Thank you
EDIT
DELIMITER $$
CREATE PROCEDURE insertOnline (IN Name VARCHAR(30))
BEGIN
DECLARE playerid INT;
INSERT IGNORE INTO players (name) VALUES (Name);
SET playerid = (SELECT id FROM players p WHERE p.name = Name);
INSERT INTO playersonline (playerid) VALUES (playerid);
END$$
DELIMITER ;