INSERT with SELECT in mysql

Asked

Viewed 79 times

0

I just created a field on my table reporterio_psi called convenio.

I have a table called patients, which also has a field called convenio, where they have data in this field.

In these two tables, they have a field that the data is equal. pacientes & nomes

I’m trying to pass on the field data convenio to the table reporterio_psi and for some reason I’m not getting...

Code:

INSERT INTO relatorio_psi(convenio) 
 SELECT convenio from pacientes 
  WHERE relatorio_psi.paciente = pacientes.nome;

The fields convenio are of the VARCHAR type.

ERROR: #1054 - Unknown 'reporio_psi.patient' column under 'Where clause'

Database

  • 1

    What a mistake you’re making?

  • #1054 - Report column 'psi.patient' unknown in 'Where clause'

  • The table relatorio_psi has the patient `column``?

  • Yes... database

  • It wouldn’t be an update you’re trying to do?

  • I don’t think so, no data in the field convenio table reporterio_psi

  • All right, the field is new, it won’t even have data, but the report table_psi has patient data?

  • Yes, see there... Strange guy, I don’t know

Show 3 more comments

1 answer

2


I don’t know what your database is, but try this SQL:

UPDATE RELATORIO_PSI SET CONVENIO=(SELECT CONVENIO FROM PACIENTES
WHERE RELATORIO_PSI.PACIENTE=PACIENTES.NOME)

Anything warns me.

Hug

  • Thanks, it worked... I thought it was strange my not working, I always did this way I did and always worked, which will be???

  • I was riding here, but it has already been solved: http://sqlfiddle.com/#! 9/fb0460/1

  • Thank you @Kaduamaral for trying!

  • @Leticia: There’s nothing strange. From what you informed you have created a new field in your table and in this table there is already a record for this patient so what you need to do is update this patient’s record and not insert a new record.

  • No wonder @Leticía, I always did this way I sent you. I don’t know this way you were doing.

  • 1

    @Fabianobridges maybe because you use another bank...

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.