Retrieve data from fields of the same name but from different tables

Asked

Viewed 1,120 times

2

I have a select that looks for data from three different tables, but has some common fields between these three tables. My question is how to get the data of these fields, which has the common name with the other tables.

My select is like this:

    SELECT suporte_cad.reg,suporte_cad.data,suporte_cad.status,
    suporte_cad.nmcontato, suporte_cad.mensagem ,
    serv_cad.nmgrupo,serv_cad.dsservico, suporte_det.reg, 
    suporte_det.data, suporte_det.nmContato, suporte_det.nmUsuario, 
    suporte_det.mensagem

    FROM suporte_cad,serv_cad,suporte_det where cnpj='$getCnpj'
    and suporte_cad.idservcad=serv_cad.idservcad and suporte_det.reg='$reg'
    and suporte_cad.reg = '$reg'

I want to get the message field from the tables support_cad and support_det

  • I don’t understand, you’re taking the fields you want.

  • 1

    SQL AS http://www.w3schools.com/sql/sql_alias.asp

  • 1

    @bigown I’m guessing the problem is at the other end (PHP?) when reading the results by column name.

  • @bfavaretto killed, I think it’s right, he would use elsewhere, maybe even in the JOIN that would be put after.

  • 1

    ALIAS solved me, did exactly what I wanted

1 answer

5


Use a alias in each of these fields, which you define with AS:

SELECT 
    suporte_cad.reg,
    suporte_cad.data,
    suporte_cad.status,
    suporte_cad.nmcontato, 
    suporte_cad.mensagem AS mensagem_cad,
    serv_cad.nmgrupo,
    serv_cad.dsservico, 
    suporte_det.reg, 
    suporte_det.data, 
    suporte_det.nmContato, 
    suporte_det.nmUsuario, 
    suporte_det.mensagem AS mensagem_det
...

Browser other questions tagged

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