-1
Staff I have the following situation, I have 2 tables, I need to insert in table B data that are in table A, however, should be checked if the data that are in table A already exist in table B, if there should be disregard, can only enter if there is no data.
I am trying with the following query:
INSERT INTO TABELA B (
n_notif_esus_ve,
data_notificacao,
data_inicio_sintomas,
exames,
nome,
nome_mae,
sexo,
data_nasc,
idade,
raca_cor,
endereco,
bairro,
telefone,
cpf_api,
cns_api
)
SELECT
n_notificacao,
data_notificacao,
data_inicio_sintomas,
tipo_teste,
nome,
nome_mae,
sexo,
data_nasc,
idade,
raca_cor,
endereco,
bairro,
telefone,
cpf,
cns
FROM
TABELA A
WHERE
(SELECT * FROM TABELA A y
WHERE NOT EXISTS (SELECT * FROM TABELA B x WHERE y.nome = x.nome AND y.data_nasc = x.data_nasc
));
I separately tested the Insert and the query that contains the Where not exists, both of which work, but when I put Where not exists as a condition, I get the following error:
Error Code: 1241. Operand should contain 1 column(s)
I appreciate anyone who can help, obg.
You should compare only 1 column within the
where
, some primary key maybe, I don’t know how your tables are structured.– ThRnk
Your subselect is returning all fields of all TABLE A rows whose name/date of birth does not exist in TABLE B. This is not a condition (the result of which is true or false).
– anonimo
Hi, @Thrnk, are two tables that have no relation, 1 table is data that comes from the external API, and the other is the table that contains data that is manipulated by users. @anonimo, really, I made a change with the following query that does pretty much the same thing, only I used distinct to avoid duplicates: The sub query looks like this..
(select distinct nome, data_nasc from tabela B
where (nome,data_nasc) not in 
(select distinct nome, data_nasc from tabela A));
But the same error persists :(– Emanuel Vitor