Change column name "id" of an sql return

Asked

Viewed 692 times

0

I want to conduct a consultation SQL and change the name of the columns in the result because I have 3 columns id.

Currently my SQL this way:

SELECT * 
FROM shopweb_tipo as st 
INNER JOIN shopweb_categoria AS sc 
INNER JOIN shopweb AS s 
WHERE s.categoria = sc.id 
    AND s.tipo = st.id

I tried to do so but I could not result.

SELECT * 
FROM shopweb_tipo as st 
INNER JOIN shopweb_categoria AS sc 
INNER JOIN shopweb AS s 
WHERE s.categoria = sc.id 
    AND s.tipo = st.id 
    AND shopweb.id as "id_shopweb"

1 answer

1


You have to describe the columns instead of putting the * and give an alias for them example:

SELECT
    st.id AS [apelido para o id],
    [demais_campos st],
    sc.id AS [apelido para o id],
    [demais_campos sc],
    s.id AS [apelido para o id],
    [demais_campos s]
FROM
    shopweb_tipo AS st
INNER JOIN shopweb_categoria AS sc
INNER JOIN shopweb AS s
WHERE
    s.categoria = sc.id
AND s.tipo = st.id

Sure, and call the other fields in the query also.

Another way is to search only the id field and then the other fields, but the ID field will come twice, one with alias other

SELECT
    st.id AS [apelido para o id],
    st.*,
    sc.id AS [apelido para o id],
    sc.*,
    s.id AS [apelido para o id],
    s.*
FROM
    shopweb_tipo AS st
INNER JOIN shopweb_categoria AS sc
INNER JOIN shopweb AS s
WHERE
    s.categoria = sc.id
AND s.tipo = st.id
  • But it would not be very feasible, imagine each table containing 9 to 15 fields and add up an average of 40 fields, it would be a huge query.

  • 1

    you can repeat the field ID? because then could do otherwise, already edit the answer

  • only the ID fields yes, why I need each separate id and when I pull with php I don’t know which one I’m picking up.

  • So with the second example, it solves your problem

  • Yes solved, was exactly what I needed, no problem for me id repeat since in the application I specify.

  • All right, if you can accept my answer, I’d appreciate it ;)

  • Waiting for the time to accept.

Show 2 more comments

Browser other questions tagged

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