Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9092503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:51:37+00:00 2026-06-16T22:51:37+00:00

I have a table containing several columns, one of which is a date column

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:51:38+00:00Added an answer on June 16, 2026 at 10:51 pm

    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>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a MySQL table, I have several columns - some containing strings, some dates
I have a table with several hundred records, each one containing a datetime column.
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I have PostgreSQL table with several boolean columns, currently containing only true or null
I have a MySQL table in which a column contains string prefixes. For instance
I have a MySQL table containing several fields; like this: id name ton dyr
I have a table that contains several rows each containing one button and one
I have a database with several tables, each one containing columns that may not
I have a table containing reports and the date/time they were created. I'd like
I have a table containing 2 date fields and an identifier (id, fromdate and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.