Difficulty running a Select in Postgresql

Asked

Viewed 48 times

0

I am having a difficulty in making a select in postgresql, in the select in question, I would like to display the name of distinct cities related to two distinct tables, below follows the DER and the select script I tried to develop, but it didn’t work as expected because it showed the city id’s when you expected to see their name:

inserir a descrição da imagem aqui

    -- LISTA DE FRETES REALIZADOS, MOSTRANDO: O NOME DO CLIENTE, A CIDADE DE ORIGEM, CIDADE DE DESTINO, O PRODUTO ENVIADO E O VALOR PAGO PELO FRETE:
SELECT clientes.nome "Nome do Cliente", frete.cidade_origem "Cidade de Origem", end_entrega.cidade_entrega "Cidade de Destino", frete.descricao "Produto", frete.valor "Valor do Frete"
FROM frete INNER JOIN clientes ON frete.idcli = clientes.idcli
INNER JOIN end_entrega ON clientes.idcli = end_entrega.idcli INNER JOIN cidade ON cidade.idcid = frete.cidade_origem

1 answer

0


is showing the ids why you connected the city table to the origin, but is showing the city_origin of the freight table and not the city table name field, and missed connecting the city table with the end_delivery.city_delivery table to show the destination city.

Try running select below to see if it works, I left the old fields commented so you can compare.

SELECT 
    clientes.nome 'Nome do Cliente',
    -- frete.cidade_origem 'Cidade de Origem',
    cidade.nome 'Cidade de Origem',
    -- end_entrega.cidade_entrega 'Cidade de Destino',
    cidade_ent.nome 'Cidade de Destino',
    frete.descricao 'Produto',
    frete.valor 'Valor do Frete'
FROM
    frete
        INNER JOIN
    clientes ON frete.idcli = clientes.idcli
        INNER JOIN
    end_entrega ON clientes.idcli = end_entrega.idcli
        INNER JOIN
    cidade ON cidade.idcid = frete.cidade_origem
        INNER JOIN 
    cidade cidade_ent ON cidade_ent.idcid = end_entrega.cidade_entrega
  • Failed, error: ERROR: schema "clients" does not exist *********Error ************* ERROR: schema "clients" does not exist SQL state: 3F000

  • please send the script to create the tables with some data for me to test, because in the database template you have above, the client table exists yes.

  • So Pablo, I did an analysis of the code you sent me to solve the problem and from it I was able to make an adaptation that resulted in the correct resolution of my problem. My thanks.

  • @Legal flaviolucasfernandes that worked, please signal my response as positive.

Browser other questions tagged

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