Subselect within a field

Asked

Viewed 356 times

-1

I am displaying several fields through a SELECT:

SELECT razaosocial, nomefantasia, cnpj, ie, im, endereco, endereco_numero, endereco_complemento, codCedente FROM cliente WHERE razaosocial LIKE '%carvalho%'.

So far, so good. But there I also display the codCedente, which comes numeric, I can do a SELECT there inside the codCedente to find the social reason inside a table called CEDENTE? (select razaosocial from cedente Where codCedente = 'codCedente') codCedente.. let’s say it that way?

1 answer

1


Explanation

I believe that the easiest way (I believe more performative too) to perform this consultation would be to use the JOIN with the table Transferor.

Query

SELECT 
    c.razaosocial, 
    c.nomefantasia, 
    c.cnpj, 
    c.ie, 
    c.im, 
    c.endereco, 
    c.endereco_numero, 
    c.endereco_complemento, 
    ce.razaosocial
FROM 
    cliente c 
    INNER JOIN cedente ce on ce.codCedente = c.codCedente
WHERE 
    razaosocial LIKE '%carvalho%'
  • Yes, I would do it myself. However, this is not the goal! I need to select directly in the field, because I pass these parameters via get, from which fields the system should compose to display.

  • if($value=="billing_data"){ $fields .= " , (SELECT razaosocial FROM cedente WHERE idCedente = codCedente) AS codCedente, email_financeiro, vencimento, forma_de_pagamento, nota, fechamento_de, fechamento_a, guias"; } - Fiz dessa forma.

  • 1

    Already solved your problem with sub-select in the column?

  • Yes, I’ve solved it

Browser other questions tagged

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