R
R
Rinat Bakiev2015-10-29 08:59:07
MySQL
Rinat Bakiev, 2015-10-29 08:59:07

How to correctly compose a SQL query (table search)?

There are several tables in one database
number_owner
id, number_table_id, contragents_id
number_table
id, number ....
contragents
id, inn ....
number_table and contragents have data. The number_owner type links these two tables in the database. what sql query should be when i have a separate csv file which has columns:
inn, number
i.e. I eventually need to make an insert into the number_owner table, but at the same time specifying number_table_id, contragents_id, where contragents_id is taken from the inn field, and number_table_id, respectively, from number

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Entelis, 2015-10-29
@DmitriyEntelis

If I understand you correctly, you already have some lists number in the number_table table, inn in the contragents table and there is a separate list of records in csv that need to be normalized.
IMHO the simplest and most correct solution is to write a small script in any PL.
Depending on the amount of data in existing tables, you can honestly pull them every time, or load them into memory.

D
Dmitry Kovalsky, 2015-10-29
@dmitryKovalskiy

1) you need to do a SELECT into the temporary csv content table
2) And then something like

INSERT INTO number_owner(number_table_id,contragent_id)
SELECT nt.id, c.id FROM @tempTable as t
INNER JOIN contragents as c ON t.inn = c.inn
INNER JOIN number_table as nt ON t. number = nt.number

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question