This question already has answers here:
Get the new record primary key ID from MySQL insert query?
(13 answers)
Closed 3 years ago.
INSERT INTO db.a (a,b,c,d,e,f,g,h,i)
VALUES (2,2,"a",'b','c','d',1,'e',0)
The first insert will insert a new row with a incremental_id. I need to take that incremental_id and make an entry into db.b using that incremental_id, for example, if it was 6005 I would make the following insert.
INSERT INTO db.b
(a_id, s_id, use_i)
VALUES (6005,7,0)
How can I automatically grab the id of the first insert so my second insert can be built dynamically after the first query?
You can use function LAST_INSERT_ID() which will return exactly what you need.
Related
This question already has answers here:
Avoid duplicates in INSERT INTO SELECT query in SQL Server
(11 answers)
Closed 1 year ago.
In sql I have one blank table and second one with values
In me second one I have column with values like
poland-data
russia-data
usa-data
england-data
poland-data-hr
england-data-hr
england-hr
poland-hr
I want to copy to the blank table column with values without 'hr' from table nr.2
for example i only want to see in that table
poland-data
russia-data
usa-data
england-data
You can try following approach:
SELECT distinct REPLACE(col1 ,'-hr', '') as col1
INTO TableTwo
FROM TableOne
It's not entirely clear what you are asking. Is this a specific case of something that will have to be done many times with variable columns? If not, you should be able to just to
CREATE TABLE TABLE_2 AS
SELECT poland_data,
russia_data,
usa_data,
england_data
FROM TABLE_1;
Or
INSERT INTO TABLE_2
SELECT poland_data,
russia_data,
usa_data,
england_data
FROM TABLE_1;
If you already have the table created.
This question already has an answer here:
Is LAST_INSERT_ID() in a transaction dependable?
(1 answer)
Closed 5 years ago.
When using transactions for the code below, is there any risk of the LAST_INSERT_ID() not belonging to the first insert? That is, is it possible for another insert action to take place, say from different user interactions like INSERT INTO comments (comment) VALUES ('kthanksbuy');, which would then give LAST_INSERT_ID() the comments ID instead of the user's ID?
BEGIN;
INSERT INTO users (username, password)
VALUES('john', 'pass');
INSERT INTO profiles (userid, bio, homepage)
VALUES(LAST_INSERT_ID(),'I am a lumberjack, and I am ok!', 'http://www.stackoverflow.com');
COMMIT;
Thanks.
LAST_INSERT_ID() - Value of the AUTOINCREMENT column for the last INSERT.
Or more broad: LAST_INSERT_ID() returns a BIGINT UNSIGNED (64-bit) value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement. It is from official documentation.
And it is definitely not first insert...
This question already has answers here:
LAST_INSERT_ID() MySQL
(14 answers)
Closed 5 years ago.
I am working on a query that I'd like to run to import some phrases based on actions.
These are the two tables I want to insert data into.
actions:
- id
- name
- data
phrases:
- id
- action_id
- phrase
Where phrases.action_id = actions.id
I want to create a new action. Return that ID and then add multiple phrases using the returned ID.
Is there anyway of making the action.id that was inserted persistent or a variable for re-use.
My train of thinking has led me to things like:
SELECT LAST_INSERT_ID();
And
OUTPUT insterted.id
Not expecting an answer but some helpful information that will point me in the right direction would be great
You can assign it to a user variable:
INSERT INTO actions ...;
SET #action_id = LAST_INSERT_ID();
Then you can use #action_id in all the INSERT queries that insert into phrases.
INSERT INTO phrases (action_id, phrase) VALUES (#action_id, "Whatever");
INSERT INTO phrases (action_id, phrase) VALUES (#action_id, "Some other phrase");
You could also solve it by doing all the inserts in a single query:
INSERT INTO phrases (action_id, phrase) VALUES
(LAST_INSERT_ID(), "Whatever"),
(LAST_INSERT_ID(), "Some other phrase");
This question already has an answer here:
INSERT deleted values into a table before DELETE with a DELETE TRIGGER
(1 answer)
Closed 6 years ago.
How can I achieve to Delete a row before Insert a new one in the same Table. I tried it with a Trigger but I read that it is not possible because it could cause a deadlock.
I also wanted to save the row which should be deleted to another table (example Table B) before delete it and then Insert a new one (into Table A).
Is there any other ways to do it ?
PS: They will have the same key
You could use UPDATE...
UPDATE tbl
SET col1 = newCol1,
col2 = newCol2
WHERE etc = etc
And If you want to insert updated row to another table you could use TRIGGER AFTER UPDATE for that.
CREATE TRIGGER TriggerName ON Tbl
AFTER UPDATE
AS
INSERT INTO Log (Col1, Col2)
SELECT Col1, Col2
FROM deleted
This question already has answers here:
mysql insert if value not exist in another table
(5 answers)
Closed 8 years ago.
INSERT INTO entity (name,surname,adr_id) values('test','test',< pseudocode see bellow >)
I want to check in table addresses by street,town and return that ID as adr_id or insert a new row in addresses.
So initially INSERT to have existing adr_id or a new, just created one.
This is possible?
You can use Insert ... On Duplicate... Update...
Example :
INSERT INTO table (a,b,c) VALUES (4,5,6)
ON DUPLICATE KEY UPDATE c=9;