I have 3 tables members fees and tblinvoice All they have a memberid
So if someone pays data goes to fees and tblinvoice and invoiceid is created in a fees table and tblinvoice plus memberid is inserted in these two tables two know which member paid.
now when the invoice is created only the tblinvoice recieves data but still with the memberid.
Now I want to pull out data from the 3 tables but the two tables fees and tblinvoice data will be based on the date range from tblinvoice which is invoicedate and also from tblinvoice date from fees fees.paiddate.
Now if there was data in table fees put it and if there is nothing pull out from tblinvoice or if both pull out from both.
The objective here is to pull out a statement for payments and invoiced amount. I wonder if I explained well this is what I have tried below am using mysql.
select
members.memberid,
fees.feesid,
members.usercode,
members.title,
members.name,
members.surname,
members.address,
members.city,
members.cell,
fees.recieved_by,
fees.paidfor,
fees.fromdate,
fees.amountpaid AS amountpaidMe,
fees.feestype,
fees.qty,
fees.paiddate,
fees.unitPrice,
fees.todate,
fees.invoiceid,
tblinvoice.amount AS invoicedAmount,
tblinvoice.invoicedate
FROM members
INNER JOIN fees ON fees.memberid = members.memberid
LEFT OUTER JOIN tblinvoice ON tblinvoice.memberid = fees.memberid
WHERE members.memberid ='62396'
&& tblinvoice.invoicedate BETWEEN '2023-01-01' AND '2023-03-05' OR fees.paiddate BETWEEN '2023-01-01' AND '2023-03-05'
I was asked to share all the data in the three tables and below is the data.
Bu if I trie to query using the date ranges it get everytihng wrong
I want to get all data from the fees tabale base on paiddate range and all data from invoice table based on invoicedate range is one table has no data just make that line black then pull out the data in another table
Query Members
SELECT name from members where memberid = '62396'
Results
Name
Abdullah Bin Salaam
Query fees table
SELECT `amountpaid` , `paiddate` , `memberid` FROM `fees` WHERE memberid = '62396'
results
Full texts amountpaid paiddate memberid
Edit Edit Copy Copy Delete Delete 1300.00 2022-08-18 11:44:44 62396
Edit Edit Copy Copy Delete Delete 1300.00 2022-09-13 03:56:50 62396
Edit Edit Copy Copy Delete Delete 650.00 2022-11-15 04:12:34 62396
Edit Edit Copy Copy Delete Delete 1500.00 2022-12-07 09:37:13 62396
Edit Edit Copy Copy Delete Delete 1300.00 2023-01-13 06:00:58 62396
Edit Edit Copy Copy Delete Delete 1300.00 2023-01-24 05:55:11 62396
Edit Edit Copy Copy Delete Delete 1300.00 2023-02-03 08:36:36 62396
Edit Edit Copy Copy Delete Delete 1300.00 2023-02-21 08:38:53 62396
Query tbl invoice table
SELECT `amount`, `memberid`, `invoicedate` FROM `tblinvoice` WHERE memberid = '62396'
results
Full texts amount memberid invoicedate
Edit Edit Copy Copy Delete Delete 340.00 62396 2023-03-01 18:27:18
Edit Edit Copy Copy Delete Delete 89.00 62396 2023-03-01 18:28:41
Edit Edit Copy Copy Delete Delete 88.00 62396 2023-03-01 18:28:41
Edit Edit Copy Copy Delete Delete 21.00 62396 2023-03-02 09:22:01
Edit Edit Copy Copy Delete Delete 55.00 62396 2023-03-02 09:22:01
Edit Edit Copy Copy Delete Delete 890.00 62396 2023-03-02 11:51:27
2
Answers
Instead of trying to limit the rows from Fees or Invoices in the final where clause, try including the date ranges into the join conditions. e.g.
Note, if you have a left joined table, then use columns from that table in the where clause you can cause the effect of an inner join unless NULLs due to unmatched rows are explicitly permitted. By moving the date range filters into the joins you avoid this added complexity.
Reverse engineered DDL & sample data with operating query (as seen above) can be found here: https://dbfiddle.uk/35z4cps8
You may have more success by trying a union of invoices and fees: e.g.
Result
DDL & DML
see: http://sqlfiddle.com/#!9/6769ff/5