我有一个包含多个列的表,其中一列是日期列(data1)。
使用的mysql查询是
SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()
然后我们从每一行获取数据,运行一些计算并将其存储为单独的变量。
我现在想将这些数据与上周的数据(在同一张表中)进行比较,即通过更改SELECT查询。
让我扩大......
查询从表中获取本周数据:
$sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()";
现在我们开始提取数据
$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>
我现在想更改上面的$ sqld查询以选择表格中属于上周的行,运行上面相同的计算并显示结果下面我们可以比较两者。
<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>
有没有办法实现这一点而无需复制所有内容并更改$ sqld查询?
答案 0 :(得分:0)
$result1 = mysql_query("SELECT * FROM leads");
$result2 = mysql_query("SELECT * FROM other table");
$arr1 = new Array();
$arr2 = new Array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$arr1['FirstName'] = $row['your column name'];
}
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$arr2['FirstName'] = $row['your column name'];
}
现在你有两个数组($ arr1和$ arr2),你可以根据需要进行比较......
答案 1 :(得分:0)
我认为您可以创建一个重复数据的函数,并使用某个参数调用它来更改查询,如下所示
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>
}