Bring result from a select that depends on another

Asked

Viewed 23 times

0

Good morning, I have a question regarding SQL. Currently I have 2 querys where one needs the result of the other to return everything I need, and I wanted to know if I can transform the 2 querys in just 1 query. Below I will leave both querys to be analyzed.

This my first query returns some fields I need, the most important there is the ballast, because I need it in the second query to return the rest of the results, remembering that I make a Join to separate the note types I need in each query.

SELECT
        pedido.codigo AS pedido_origem,
        DATE_FORMAT(FROM_UNIXTIME(pedido.data), '%d-%m-%Y') as data_pedido_origem,
        pedido.nf_numero AS numero_nf_origem,
        nota_devolucao .nf_numero AS numero_nf_devolucao,
        FORMAT(nota_devolucao.valor_total,2,'de_DE') AS valor_nf_devolucao,
        pedido.id_lastro as lastro
    FROM
        kb_pedidos AS pedido
        INNER JOIN kb_notasfiscais AS nota_devolucao ON (pedido.codigo = nota_devolucao.pedido_numero AND nota_devolucao.operacao = 'E')
    WHERE
        $where_filtro
        AND pedido.id_lastro != ''
    ORDER BY
        pedido.codigo DESC 

This is the second query that needs the ballast of the first to get me back the rest of the results.

SELECT
        nota_nova.nf_numero AS numero_nf_nova,
        FORMAT(nota_nova.valor_total ,2,'de_DE') AS valor_nf_nova,
        pedido.codigo as pedido_novo,
        DATE_FORMAT(FROM_UNIXTIME(pedido.data), '%d-%m-%Y') as data_pedido_novo
    FROM
        kb_pedidos AS pedido
        INNER JOIN kb_notasfiscais AS nota_nova ON (pedido.codigo = nota_nova.pedido_numero AND nota_nova.operacao = 'S')
    WHERE
        pedido.id_pedido = ? 
    ORDER BY 
        pedido.codigo DESC
    LIMIT 1 

Would it be possible so join both querys so I get all results in one place ?

No answers

Browser other questions tagged

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