'mySQL query to insert the last incremented id from two tables into one
I am trying to figure out how to get the last incremented id from two different tables and insert those values into a third table. I have a working query for when it was only one table, utilizing last_insert_id(), but i am stumped on how to get something like last_insert_id() but getting it from two different tables.
(I am unable to post images on here, so here is a link to the 3 tables: https://imgur.com/a/cbHlHAW)
In my schema I want it so a single user can post multiple items.
The ids for user_id and item_id will most likely be different values from each other.
Here is my current attempt at trying to figure it out.
begin;
INSERT INTO post_items(name,description,category,city,state,zip,item_condition)
VALUES('test','test','test','test','test',12345,'test');
INSERT INTO users(city,state,zip)
VALUES('test','test',12345);
INSERT INTO links(date_posted,link,item_id,user_id)
VALUES(now(), 'google.com',post_items.last_insert_id(),users.last_insert_id());
Commit;
I have try researching on how to get it to work, but no luck.
Before I added the table users and added user_id to the links table I had a working query to insert into both tables just the way I wanted it to.
INSERT INTO post_items(name,description,category,city,state,zip,item_condition)
VALUES('test','test','test','test','test',12345,'test');
INSERT INTO links(date_posted,link,item_id)
VALUES(now(), link,last_insert_id());
I know how I would make a select statement but I can't get a working insert statement.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
