M
M
medovuk2017-10-13 23:04:56
MySQL
medovuk, 2017-10-13 23:04:56

How to insert a key field from another table when inserting data into another table?

There are two tables: A(id, name, ....), B(id, ....). Table A is, as it were, a source of information (let's say goods with their characteristics), table B will contain additional information. information, the B.id field is one of the composite keys. How to insert data into table B(1, ....), take this id from table A. That is, A(1, "books", ...), insert into B(id, ....) values (one, ....,)? I did this:
insert into B ( id, ...) values ​​((select id from A where Name = "books"), ....));
If the way is better? The data is taken from the file to both tables, added to A if there is no such record, and always to B.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Y, 2017-10-19
@x_shader

The disclosure of the primary key (ID) by the alternative (Name) for the case of loading data is something like this, as you are doing. The implementation may differ, but the essence is the same.
Provide another table for parsing errors, where lines will be thrown off for which a match for your "Name" was not found, or more than one line was found.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question