0

I have three tables customers, orders, and payments. payments table has a foreign key order_id, which references the orders table on the id field, however, the payments table can also accept payment without any order, i.e.( advance payment received without any order being placed.) conversely it is also possible that there could be orders without any payment e.g. orders created on credit without any payment.

now my query is I want to generate a day-closing table where I want to list all orders if any or all payments if any for a particular date.

I have tried to join the orders table with the payment table on order_date and payment_date but the result is not as per my requirement. i.e. if there are no orders on a particular date but there are payments then I don't get any results.

I have created a scenario on dB fiddle for the same. dbfiddle

if anybody can give me a solution, it will be an immense help.

as per the scenario on dB fiddle I tried the following query.

select 
      o.id, 
      o.order_date, 
      o.customer, 
      o.inv_number, 
      p.pymt, 
      p.cash, 
      p.bank 
   from 
      orders o 
         left join 
         (select 
               pymt_date, 
               sum(amount) pymt, 
               sum(cash) cash, 
               sum(bank) bank 
            from 
               payments 
            group by 
               pymt_date) p 
      on 
         o.order_date = p.pymt_date 
   where 
      o.order_date = '2022/10/25'

but I am not getting any results.

DRapp
  • 47,638
  • 12
  • 72
  • 142
raj
  • 9
  • 3
  • TL;DR but there are different kinds of joins like `LEFT JOIN` where NULLs appear for the pottentially joined records that are not found; leaving only one table's records. – Joop Eggen Nov 30 '22 at 07:56
  • I have tried a lot of sir but could not achieve the desired result. please have a look at dbfiddle linke https://www.db-fiddle.com/f/x3k7gWWncxzE7ox7UWrYZY/7 – raj Nov 30 '22 at 08:02
  • 1
    Does this answer your question? [How can I do a FULL OUTER JOIN in MySQL?](https://stackoverflow.com/questions/4796872/how-can-i-do-a-full-outer-join-in-mysql) – P.Salmon Nov 30 '22 at 08:06
  • @raj no time sorry, hope P.Salmon may shed some light – Joop Eggen Nov 30 '22 at 08:39

0 Answers0