Merge two php+mysql query tables

Asked

Viewed 507 times

1

I have two tables: bills payable and receivables. The two tables have: code, description, date, payment date, date of receipt and value.

Like I make a query to take the data of the two tables sorting by the date?

I was using the Union:

SELECT 
    codigo,
    descricao,
    dataVencimento,
    dataEntrada,
    valor
FROM
    contasReceber
WHERE
    status= "pago" 

    UNION ALL

SELECT 
    codigo,
    descricao,
    dataVencimento,
    dataSaida,
    valor
FROM
    pagamento
order by dataVencimento

But I couldn’t think of a way to differentiate the inputs of the exits. Because I need to display this data, for example:

Codigo   Descrição   Pago    Recebido
1        farmacia    20,00   -
2        aluguel     -       30,00  

How can I do it using javascript+php+mysql?

1 answer

1


continue using Union, but you should create additional columns:

SELECT 
r.codigo,
r.descricao,
r.dataVencimento,
r.dataEntrada,
0 as pago,
r.valor as recebido,
'CR' as tipo
FROM
contasReceber r
WHERE
r.status= "pago" 
and r.dataVencimento between '01/08/2017' and '30/08/2017'

UNION

SELECT 
p.codigo,
p.descricao,
p.dataVencimento,
p.dataSaida,
p.valor,
0,
'PG' as tipo
FROM
pagamento p
where p.dataVencimento between '01/08/2017' and '30/08/2017'

order by dataVencimento

remembering that the column name will follow the name of the first query.

  • I didn’t understand how this query will work. Can you please explain? What is this 0 the received and this 0 paid? And this column type?

  • @L.J Sorry for the delay, I was traveling, that 0 received, will generate a column with value 0 when the row comes from the accounts receivable table, and also for the other table. The type, just to differentiate the rows, and show you that you can generate other columns in select. When viewing the query result, you can know which row came from which table.

  • Then it should not be in the first query 0 as paid pq it will ta selecting the accounts it will receive. And at the bottom that he is selecting the payments should not be 0 the received, since he is selecting payments so the received column has that 0?

  • Got it now. What if I want to filter the due date or date between two dates. How would I use the BETWEEN in this query? Fis isso: dataexpiration between '01/08/2017' AND '30/08/2017' but is not working. It is giving error: Unknown column 'dataPackage' in 'Where clause'

  • This was supposed to work but is not working. Unknown column 'dataexpiration ' in 'Where clause'

  • @L.J tests now.

  • Gave this error: Unknown column 'r. date 'in 'Where clause'

  • are you sure this column exists in your table ? puts their structure there please

  • It worked...was because I was using date format and was using DATE_FORMAT(r.expiration_expiration,'%d/%m/%Y') AS dataexpiration. Then I was getting dated. and not r.data_expiration Ai was giving this stick but now funfou. Vlw guy!!!

Show 4 more comments

Browser other questions tagged

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