I have a DB with discount card records, i need to copy the info from main DB to other with the same struckture. Need to update the fields name, phone etc from the main DB. The info i need is in three tables: 1.pers_cards (the card numbers). 2.pers_clients (the name, phone, etc). 3 pers_accounts (the table combine two previous table by fields client_id and account_id) I wrote script to get the info i need:
Copy (SELECT pers_cards.card_num, pers_clients.name, pers_clients.address,
pers_clients.phone, pers_clients.mobilephone, pers_clients.email,
pers_clients.birthday,pers_clients.gender_male
FROM pers_cards,pers_accounts,pers_clients
WHERE (
pers_accounts.client_id = pers_clients.client_id
and
pers_cards.acc_id = pers_accounts.acc_id
))
To 'f:/test.csv' With CSV;
Now i need to update the other BD using this file, update fields with personal info (pers_clients.name, pers_clients.phone etc) depending of its card number. trouble is the fields client_id and account_id has different values in different tables.
CREATE TABLE cards_temp
(card_num varchar, name varchar, address varchar,
phone varchar, mobilephone varchar, email varchar, birthday date, gender_male bool);
COPY cards_temp FROM 'f:/test.csv' DELIMITER ',' CSV;
UPDATE cards cards
SET name = cards_temp.name
FROM cards_temp
WHERE cards_temp.card_num = cards.card_num;
How to update my main table:
UPDATE pers_clients
SET name = cards_temp.name
FROM cards_temp
WHERE cards_temp.card_num = (SELECT pers_cards.card_num
FROM pers_cards,pers_accounts,pers_clients
WHERE (
pers_accounts.client_id = pers_clients.client_id and pers_cards.acc_id = pers_accounts.acc_id ));
mysqlwhy?postgresqlandmysqltags.