从具有连接和联合的各种表中选择记录

时间:2014-03-08 14:27:55

标签: php mysql sql

为了学校学生的分类帐报告 -
我如何得到这样的结果:(对于学生 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    |
| .. | ...    | .....      | ..   | ...    |
+----+--------+------------+------+--------+

的信息:

  • 向外日期日期
  • 费用。期限详情
  • outward.id id 或receipt.id id
  • fees.ount 借记
  • receipt.amount 信用
  • 结果应为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

但它在this SQLfiddle

中看不到

请查看我以前的工作/问题HERE

0 个答案:

没有答案