切换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?

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>
}
$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'];
  }

now you have your two arrays($arr1 and $arr2) and you compare as per you want...