donglinyi4313 2013-01-03 12:45
浏览 20
已采纳

切换mysql查询以比较结果

I have a table containing several columns, one of which is a date column (data1).

The mysql query used is

SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()

We then take the data from each row, run some calculations and store this as a separate variables.

I would now like to compare this data with data from last week (in the same table), i.e. by changing the SELECT query.

Let me expand...

Query for getting this weeks data from table:

$sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()";

Now we run through extracting the data

$result = mysql_query($sqld) or die(mysql_error());

$num_rows = mysql_num_rows($result);

while($row = mysql_fetch_array($result))
{
      $referred = $referred + $row['referred'];
      $invalidated = $invalidated + $row['invalidated'];
      $tobequalified = $tobequalified + $row['tobequalified'];
}

(the above is just a snippet of the calculations we need to run to demonstrate).

Now we display the results based on this weeks data

          <h4>Totals for this week</h4>

            <table class="table stat-table">
                <tbody>
                    <tr>
                        <td class="value"><? echo $num_rows; ?></td>
                        <td class="full">Total leads</td>
                    </tr>
                    <tr>
                        <td class="value"><? echo $referred; ?></td>
                        <td class="full">Referred</td>
                    </tr>
                    <tr>
                        <td class="value"><? echo $invalidated; ?></td>
                        <td class="full">Invalidated</td>
                    </tr>
                    <tr>
                        <td class="value"><? echo $tobequalified; ?></td>
                        <td class="full">To be qualified</td>
                    </tr>

                </tbody>
            </table>

I'd like to now change the $sqld query above to select rows in the table that fall into last week, run the same calculations above and display the results below so we can compare the two.

            <h4> Totals for last week</h4>


            <table class="table stat-table">
                <tbody>
                    <tr>
                        <td class="value"><? echo $num_rows; ?></td>
                        <td class="full">Total leads</td>
                    </tr>
                    <tr>
                        <td class="value"><? echo $referred; ?></td>
                        <td class="full">Referred</td>
                    </tr>
                    <tr>
                        <td class="value"><? echo $invalidated; ?></td>
                        <td class="full">Invalidated</td>
                    </tr>
                    <tr>
                        <td class="value"><? echo $tobequalified; ?></td>
                        <td class="full">To be qualified</td>
                    </tr>

                </tbody>
            </table>

Is there any way of achieving this without copying everything and changing the $sqld query?

  • 写回答

2条回答 默认 最新

  • dongqiao3927 2013-01-03 13:32
    关注

    I think you can create a function for repeting data and call it with some parameter to change the query like below

    somefunction("Previous");
    somefunction();
    
    function somefunciton($query = "current") {
        if($query == "Current")
            $sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()";
        else
            $sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 2 WEEK) and (now(),INTERVAL 1 WEEK)";
        $result = mysql_query($sqld) or die(mysql_error());
    
        $num_rows = mysql_num_rows($result);
    
        while($row = mysql_fetch_array($result))
        {
              $referred = $referred + $row['referred'];
              $invalidated = $invalidated + $row['invalidated'];
              $tobequalified = $tobequalified + $row['tobequalified'];
        }
        <h4>Totals for this week</h4>
    
            <table class="table stat-table">
            <tbody>
                <tr>
                <td class="value"><? echo $num_rows; ?></td>
                <td class="full">Total leads</td>
                </tr>
                <tr>
                <td class="value"><? echo $referred; ?></td>
                <td class="full">Referred</td>
                </tr>
                <tr>
                <td class="value"><? echo $invalidated; ?></td>
                <td class="full">Invalidated</td>
                </tr>
                <tr>
                <td class="value"><? echo $tobequalified; ?></td>
                <td class="full">To be qualified</td>
                </tr>
    
            </tbody>
            </table>
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大