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 6963041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:44:16+00:00 2026-05-27T15:44:16+00:00

I have 2 tables: The first contains data for the current day (per hour)

  • 0

I have 2 tables:

  1. The first contains data for the current day (per hour) – this table has raw data, that needs to be processed.

  2. The second contains data for previous day (per day) – this table already has the values calculated for every day.

I want to combine this to rows in a MySQL query, so that I return the data for previous dates (per day) and for current day (again, per day).

Currently I am using 2 mysql queries:

//table 1, data for current day
                    $qry1="
            select DATE_FORMAT(completedate,'%Y-%m-%d') as date, SUM(IF(complete = 1,affpayout,0)) as pay, COUNT(*) as leads
            from leads
            where affiliateid={$_SESSION["id"]}  AND completedate>'$date' 
            GROUP BY DATE_FORMAT(completedate,'%Y-%m-%d')";
//table 2, data for previous days, already processsed, we just need to select it
            $qry2="
            select DATE(date) as date, affrevenue as pay, totalleads as leads
            from leadsdays
            GROUP BY DATE(date)";

How can I combine the 2 efficiently (speed performance is an issue)? What would be the best way to do this?

  • 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-05-27T15:44:17+00:00Added an answer on May 27, 2026 at 3:44 pm

    Try to UNION. Also, make sure your tables are indexed properly. Looks like you’ll want completedate and affiliateid indexed at least. Not sure how much more efficient two queries is versus one union though…

    $qry = "(select 
        DATE_FORMAT(completedate,'%Y-%m-%d') as `date`, 
        SUM(IF(complete = 1,affpayout,0)) as pay, 
        COUNT(*) as leads
        from leads
        where affiliateid={$_SESSION["id"]}  AND completedate>'$date' 
        GROUP BY DATE_FORMAT(completedate,'%Y-%m-%d')) 
        UNION 
        (select 
        DATE(`date`) as `date`, 
        affrevenue as pay, 
        totalleads as leads
        from leadsdays
        GROUP BY DATE(`date`)) 
        ORDER BY DATE(`date`)";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a database with a few tables. The first table contains the
I have two tables, the first has a primary key that is an identity,
I have a database framework where I have two tables. The first table has
I have two tables, both with the same columns. The first table contains original
I have a database that has two tables, one of which contains a foreign
This is for MySQL and PHP I have a table that contains the following
Say I have a table or 2 that contains data that will never or
I have two tables, one contains data like this: link_id | counted ================= 1--------|
I have two SAS data tables. The first has many millions of records, and
I have a database table that contains collection data for product collected from a

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.