Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从具有联接和并集的各种表中选择记录_Php_Mysql_Sql - Fatal编程技术网

Php 从具有联接和并集的各种表中选择记录

Php 从具有联接和并集的各种表中选择记录,php,mysql,sql,Php,Mysql,Sql,为了学校学生的分类账报告- 我如何获得这种结果:(对于学生id100和日期介于2014年3月1日和2014年3月10日之间) 通过从下表中选择记录: - Table 1 = **fees** [ id - PK AI ] +----+--------------+--------+ | id | term | amount | +----+--------------+--------+ | 1 | Tuition fees | 50 | | 2 | Term

为了学校学生的分类账报告-
我如何获得这种结果:(对于学生id100和日期介于2014年3月1日和2014年3月10日之间)

通过从下表中选择记录:

- 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    |
| .. | ...    | .....      | ..   | ...    |
+----+--------+------------+------+--------+
信息:

  • export.date日期
  • 费用.期限详情
  • export.idid或receipt.idid
  • 费用.金额借方
  • 收据.金额贷方
  • 结果应该是按日期排序的
我已经试过这个问题了

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
但它并没有像你在书中看到的那样起作用


请看一下我以前的工作/问题

StackOverflow不是一个“为我做编码工作”的地方。你必须表现出一些努力!您必须向我们表明您已尝试解决您的问题,然后我们将帮助您解决您面临的具体问题。所以,为了帮助你解决这个问题,我推荐你@kingkero-我试过了,但失败了。现在我正在寻找新的开始。@Vikram这已经是一个很好的努力!我在你的问题(+做了一些格式化),你能详细说明(在问题中-不是评论)你的查询失败的地方吗?不值得被否决…:)@kingkero-当同一个学生在同一天进行对外交易时,我面临着一个问题
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