【问题标题】:How to fetch data from two tables datewise?如何按日期从两个表中获取数据?
【发布时间】:2018-05-18 14:21:42
【问题描述】:

我正在尝试从两个 mysql 表中获取数据并按日期排列它们。这两个表与收入和支出有关。我想要实现的是首先从收入表中显示 2014 年 4 月 15 日的数据,然后从支出表中显示 2014 年 4 月 15 日的数据,然后再次循环下一个日期并显示与 16/04/14 相关的数据来自收入表,然后来自支出表。当前代码循环的次数与否一样多。收入表中的条目。如何纠正这个?代码如下:

<?php
  include "connection.php";
?>
<table width="100%" border="1" align="center" cellpadding="2" cellspacing="2">
                <tr>
                  <td>Date</td>
                  <td>Particulars</td>
                  <td>Transaction Type</td>
                  <td>Sub Expense</td>
                  <td>Expense</td>
                  <td>Subincome</td>
                  <td>Income</td>
                  <td>Cash in Hand</td>
                  <td>Remarks</td>
                </tr>
                <?php
                $datequery = mysql_query("select date from income order by date asc") or die (mysql_error());
                while ($row = mysql_fetch_array($datequery)) 
                {
                $currentdate = $row['date'] ;

                echo $currentdate."<br>";

                            $result = mysql_query("SELECT *  FROM income WHERE date = '$currentdate' ") or die (mysql_error());
                            while ($row = mysql_fetch_array($result)) 
                            {
                                ?>
                            <tr>
                              <td><?php echo $row['date']; ?></td>
                              <td><?php echo $row['particulars']; ?></td>
                              <td><?php echo $row['trntype']; ?></td>
                              <td>&nbsp;</td>
                              <td>&nbsp;</td>
                              <td><?php $subincome = $row['subincome']; echo $subincome ;?></td>
                              <td><?php $income =  $row['income']; echo $income ?></td>
                              <td><?php $balance = $balance + $row['subincome'] + $row['income'] - $row['subexpense'] - $row['expense']; ;  echo $balance; ?></td>
                              <td><?php echo $row['remarks']; ?></td>
                            </tr>
                                            <?php
                            }
                                        $result = mysql_query("SELECT *  FROM expenditure WHERE date = '$currentdate' ") or die (mysql_error());
                                        while ($row = mysql_fetch_array($result)) 
                                        {
                                            ?>
                                        <tr>
                                          <td><?php echo $row['date']; ?></td>
                                          <td><?php echo $row['particulars']; ?></td>
                                          <td><?php echo $row['trntype']; ?></td>
                                          <td><?php echo $row['subexpense']; ?></td>
                                          <td><?php echo $row['expense']; ?></td>
                                          <td>&nbsp;</td>
                                          <td>&nbsp;</td>
                                          <td><?php $balance = $balance + $row['subincome'] + $row['income'] - $row['subexpense'] - $row['expense'];  echo $balance; ?></td>
                                          <td><?php echo $row['remarks']; ?></td>
                                        </tr>
                                        <?php
                                        }
                $date = strtotime($currentdate);
                $date = strtotime("+1 day", $date);;
                $currentdate =  date('Y-m-d', $date);

                }
                ?>
              </table>

【问题讨论】:

  • 您遇到的问题/错误是什么?

标签: mysql sql html-table


【解决方案1】:

您可以使用union all 运算符进行单个查询,并进行相应的排序。在下面的查询中,我添加了一个虚拟列作为排序依据,因此我可以确保收入行始终位于支出行之前:

SELECT  *
FROM    (SELECT *, 1 AS dummy FROM income
         UNION ALL 
         SELECT *, 2 AS dummy FROM expenditure) t
ORDER BY `date` ASC, dummy ASC

【讨论】:

  • 这假设您的收入和支出表具有相同数量的列、相同的数据类型、相同的顺序,或者您可以添加虚拟列来使它们如此。
  • 尝试如下,问题是我必须得到收入和支出之间的差异,而在这里我无法计算。我使用的代码。
  • 试过了,问题是我找不到收入和支出的区别。 $result = mysql_query("select *,1 AS dummy from income UNION ALL select *, 2 AS dummy from demand ORDER BY date ASC, dummy ASC"); while($row = mysql_fetch_array($result)) {echo " " 。 $row['subexpense'] 。 "";回声“”。 $row['expense'] 。 "";回声“”。 $row['subincome'] 。 "";回声“”。 $row['收入'] 。 ""; $cashinhand = $cashinhand + $row['income'] + $row['income'] - $row['subexpense'] - $row['expense'] ;回声“”。 $现金手。 "";回声“”; }
【解决方案2】:

如果您想获得老派,您可以在一个查询中检索所有按升序排序的收入记录,并在另一个查询中检索所有支出记录,也按升序排序。维护每个结果集数组的索引。随着日期的变化,逐步增加数组索引。您已经完成了merge join,这是union all .. order by 在@Mureinik 的回答中实现的。不过,按照他的方式去做;更好。

【讨论】:

  • 试过了,问题是我找不到收入和支出的区别。 $result = mysql_query("select *,1 AS dummy from income UNION ALL select *, 2 AS dummy from demand ORDER BY date ASC, dummy ASC"); while($row = mysql_fetch_array($result)) {echo " " 。 $row['subexpense'] 。 "";回声“”。 $row['expense'] 。 "";回声“”。 $row['subincome'] 。 "";回声“”。 $row['收入'] 。 ""; $cashinhand = $cashinhand + $row['income'] + $row['income'] - $row['subexpense'] - $row['expense'] ;回声“”。 $现金手。 "";回声“”; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-19
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多