I need to insert values to my table 2 with id from table 1 where name equals to 'abc'. I mean smth like this:
INSERT INTO tab2 (id, f_id, name, date)
VALUES (uuid(), (here selected id where name = 'abc'), name, date)
How can I do it? I tried smth like this:
BEGIN
DECLARE f_id char(36);
END
SET #f_id = (SELECT id FROM users WHERE email = u_email)
INSERT INTO tab2 (id, f_id, name, date)
VALUES (uuid(),#f_id,'cba',getdate())
but it doesn't work. This is just my attempt at the principle maybe it will work.
use sample code :
INSERT INTO tab2 (id, f_id, name, date)
SELECT uuid(),id,'cba',getdate() FROM users WHERE email = u_email
https://dev.mysql.com/doc/refman/8.0/en/insert-select.html
Related
My stored procedure is populating different rows and I need it to populate a single row. I have tried adding in some where clauses but that breaks it. Any help is appreciated!
DELIMITER //
CREATE PROCEDURE Populate()
BEGIN
insert into log(NoEmp) (select count(empid) from emp);
insert into log(NoDept) (select count(deptid) from dept);
insert into log(LocReg1) (select count(regionid) from region where regionid=1);
insert into log(LocReg2) (select count(regionid) from region where regionid=2);
insert into log(LocReg3) (select count(regionid) from region where regionid=3);
insert into log(TotSales) (select sum(salesamt) from sales);
insert into log(AvgSaleMo) (select TotSales/(24) from log);
insert into log(AvgSaleYr) (select TotSales/(2) from log);
insert into log(logdate) (select NOW());
END //
DELIMITER ;
Every time you INSERT it creates a new row (unless you use the ON DUPLICATE KEY UPDATE clause).
If you want to insert a single row, use a single INSERT with multiple columns and values.
INSERT INTO log (NoEmp, NoDept, LocReg1, LocReg2, LocReg3, TotSales, AvgSaleMo, AvgSaleYr, logdate)
VALUES (
(select count(empid) from emp),
(select count(deptid) from dept),
(select count(regionid) from region where regionid=1),
(select count(regionid) from region where regionid=2),
(select count(regionid) from region where regionid=3),
(select sum(salesamt) from sales),
(select sum(salesamt)/(24) from sales),
(select sum(salesamt)/(2) from sales),
NOW()
);
I need that the minimal id will be 0(zero).
I tried something like this:
INSERT INTO users(id, name)
VALUES ( (SELECT MAX(id) FROM users) + 1 , 'andrey6' )
But I have a problem when the "users" table is empty, I just cant figure out how to keep the minimal value to be 0.
You could just use COALESCE like so:
INSERT INTO users(id, name)
VALUES ( (SELECT COALESCE(MAX(id), 0) FROM users) + 1 , 'andrey6' )
Does this do what you want:
INSERT INTO users(id, name)
VALUES ( (SELECT IFNULL(MAX(id) + 1,0) FROM users) , 'andrey6' )
?
I need to make trigger to increase the 2nd digit when the new value found to be a duplicate.
For Instance, I have a unique filed with 10 digits value. I want when someone insert same number it increase the second left digit like 0100012345. How I can do that? Thank you.
FirstName LastName Code
Houssam Salim 0100012345 to be 0200012345
Try this
I have found a solution with instead off trigger
'/*
CREATE TABLE [Employee1]
(
[id] VARCHAR(20) PRIMARY KEY,
[name] VARCHAR(50)
)
CREATE TRIGGER AutoIncrement_Trigger
ON [Employee1]
instead OF INSERT
AS
BEGIN
DECLARE #ch CHAR
DECLARE #num INT
IF EXISTS (SELECT 1
FROM Employee1 e
JOIN inserted i
ON i.id = e.id)
BEGIN
SET #num=(SELECT max(CONVERT(INT, substring(e.id, 1, 2))) + 1
FROM employee1 e
JOIN inserted i
ON substring(e.id, 3, len(e.id)) = substring(i.id, 3, len(i.id)))
INSERT INTO [Employee1]
(id,
name)
SELECT '0' + CONVERT(VARCHAR(10), #num)
+ substring(i.id, 3, len(i.id)),
e.name
FROM Employee1 e
JOIN inserted i
ON i.id = e.id
END
ELSE
BEGIN
INSERT INTO [Employee1]
(id,
name)
SELECT inserted.id,
inserted.name
FROM inserted
END
END
*/
INSERT INTO [Employee1]
VALUES ('0100012345',
'John')
SELECT *
FROM [Employee1]
INSERT INTO [Employee1]
VALUES ('0100012345',
'John')
SELECT *
FROM [Employee1]
'
I searched a while for this problem, but I can't solve it...
I want to check if a record doesn't exists in a mysql table and then insert a record.
Here is my code:
IF NOT EXISTS (SELECT * FROM personOffice WHERE personID = 2 AND officeID = 1)
BEGIN
INSERT INTO personOffice (personID, officeID) VALUES ('2', (SELECT officeID FROM offices WHERE title = 'Berlin'))
END
Mysql tells me that theres a syntax-error in my first line.
Thanks for your solutions.
INSERT INTO personOffice(personID, officeID)
SELECT '2', (SELECT officeID FROM offices WHERE title = 'Berlin') FROM dual
WHERE NOT EXISTS (SELECT * FROM personOffice WHERE personId = 2 AND officeID = 1)
You might try this :
INSERT INTO personOffice (personID, officeID)
VALUES ('2', (SELECT officeID FROM offices WHERE title = 'Berlin'))
WHERE NOT EXISTS (SELECT * FROM personOffice WHERE personID = 2 AND officeID = 1)
How can I efficiently update the table based on values from a join table
only when ID - identifier I use to join both of the tables matches perfectly
1 to 1. I mean when joined table has only one ID to the updated table?
DECLARE #T1 TABLE (
ID INT,
NAME VARCHAR(10),
Age int
)
INSERT INTO #T1 VALUES (1, 'Name', null)
DECLARE #T2 TABLE (
ID INT,
Age int
)
INSERT INTO #T2 VALUES (1, 28)
INSERT INTO #T2 VALUES (1, 29)
INSERT INTO #T2 VALUES (1, 30)
In this example table T2 has three records of the ID = 1 which corresponds to one ID
in T1.
And I would like to update T1 only when in T2 there is one record of ID = 1.
(I would like to avoid joining twice table t2 to solve this task ...)
Thanks!
;WITH T2
AS (SELECT ID,
MAX(Age) AS Age
FROM #T2
GROUP BY ID
HAVING COUNT(*) = 1)
UPDATE #T1
SET [#T1].Age = T2.Age
FROM #T1
JOIN T2
ON [#T1].ID = T2.ID