So to clear up the question a bit, I have a table we’ll call it “tAuthor” and another table called “tBook”. The tables have the following columns.
tAuthor
- id – INT (PK)
- fname – VARCHAR(50)
- lname – VARCHAR(50)
tBook
- id – INT (PK)
- authorid – INT (PK)
- name – VARCHAR(50)
If I had the authors firstname and lastname and wanted to insert a new book into tBook for them, is there an easy way to have the insert statement auto fill in the authorid WHERE fname = “Known” and lname = “Known”?
I figured I could always have two statements, a select statement that returns the id WHERE fname=”Known” and lname=”Known” and then run the insert statement with the id returned. I was just curious if there was another way?
I wouldn’t recommend using an Authors name for this as there could be duplicates. However this statement will do the insert you require. You basically do a sub-select from the table you need your ID from and fill in the rest of the information as hardcoded values rather than values to retrieve. This is also a great way to do multiple updates if you need to update a bigger set of records that all share a common element.