为了学校学生的分类帐报告 -
我如何得到这样的结果:(对于学生 id 100& 日期 01-03-2014和10-03-2014)
+-------------+--------------+----+-------+--------+
| Date[d-m-y] | Particulars | id | debit | credit |
+-------------+--------------+----+-------+--------+
| 01-03-2014 | Tuition fees | 1 | 50 | |
| 01-03-2014 | Term fees | 1 | 200 | |
| 05-03-2014 | Bus Charges | 2 | 100 | |
| 10-03-2014 | Cash | 5 | | 350 |
+-------------+--------------+----+-------+--------+
从下表中选择记录:
- Table 1 = **fees** [ id - PK AI ]
+----+--------------+--------+
| id | term | amount |
+----+--------------+--------+
| 1 | Tuition fees | 50 |
| 2 | Term fees | 200 |
| 3 | Bus Charges | 100 |
| .. | ...... | ... |
+----+--------------+--------+
- Table 2 = **fees_master** [ id - PK AI ] [ perticular_id - FK ] [fees_id - FK]
+----+---------------+---------+
| id | perticular_id | fees_id |
+----+---------------+---------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 3 |
| 4 | 3 | 2 |
| .. | .... | ... |
+----+---------------+---------+
- Table 3 = **outward** [ id - PK AI ] [ studId - FK ] [ particular - FK (from bill table)]
+----+--------+------------+------------+
| id | studId | date | particular |
+----+--------+------------+------------+
| 1 | 100 | 01-03-2014 | 1 |
| 2 | 100 | 05-03-2014 | 2 |
| 3 | 205 | 01-05-2014 | 3 |
| .. | ... | ..... | .. |
+----+--------+------------+------------+
- Table 4 = **receipt** [ id - PK AI ] [ studId - FK ]
+----+--------+------------+------+--------+
| id | studId | date | mode | amount |
+----+--------+------------+------+--------+
| 1 | 100 | 10-03-2014 | Cash | 350 |
| .. | ... | ..... | .. | ... |
+----+--------+------------+------+--------+
的信息:
ORDER BY date
我已经尝试过此查询
SELECT date, term, sub, id, debit, credit
FROM (SELECT o.date,
c.term,
NULL sub,
o.id,
ch.amount debit,
NULL credit,
1 ord
FROM outward AS o
LEFT JOIN challan_terms AS c
ON o.perticular = c.headerId
AND c.type = 0
LEFT JOIN challan_header AS ch
ON ch.id = o.perticular
WHERE o.studId = 100
AND o.date >= '2014-01-20'
UNION ALL
SELECT ow.date, ct.term, ct.amount, NULL, NULL, NULL, 2
FROM outward AS ow
LEFT JOIN challan_terms AS ct
ON ow.perticular = ct.headerId
WHERE TYPE = 1
AND ow.studId = 100
UNION ALL
SELECT date, MODE, NULL, id, NULL, amount, 3
FROM receipt
WHERE studId = 100
AND date >= '2014-01-20') q
WHERE date BETWEEN '2014-01-20' AND '2014-04-20'
ORDER BY date, ord
中看不到
请查看我以前的工作/问题HERE