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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:21:54+00:00 2026-06-09T19:21:54+00:00

Im having trouble combining two MySQL statements into one, at the moment im having

  • 0

Im having trouble combining two MySQL statements into one, at the moment im having to run them each and then placing them into an array and then merging those arrays.

here are the the functions

1st function

/* returns all books from table book */
function getProfitableBooksNew(){
    $q = "SELECT *, 

    ROUND((amazon_new_price/100*80) - lowest_new_price,2) AS margin 

    FROM ".TBL_BOOKS." 

    WHERE rank < 200000 AND rank IS NOT NULL 
    AND lowest_new_price < amazon_new_price/100*80 
    AND amazon_new_price < 9999 
    AND ROUND((amazon_new_price/100*80) - lowest_new_price) > (lowest_new_price/100*".MARGIN.") 

    ORDER BY rank ASC";

    $result = mysql_query($q, $this->connection);

    if(!$result || (mysql_numrows($result) < 1))
        return null;

    $arr = array();
    $numRows = mysql_num_rows($result);//count
    if($numRows>0){
        for($i=0; $i<$numRows; $i++){
            $arr[] = array(
                "isbn"                  => mysql_result($result, $i, "isbn"),
                "title"                 => mysql_result($result, $i, "title"),
                "rank"                  => mysql_result($result, $i, "rank"),                   
                "amazon_price"          => mysql_result($result, $i, "amazon_new_price"),
                "amazon_condition"      => "New",

                "lowest_price"          => mysql_result($result, $i, "lowest_new_price"),
                "lowest_condition"      => "New",

                "margin"                => mysql_result($result, $i, "margin"),

                "last_price_updated"    => mysql_result($result, $i, "last_price_updated"),
                "last_rank_updated"     => mysql_result($result, $i, "last_rank_updated")
            );
        }
    }       

    return $arr;
}   

2nd function

/* returns all books from table book */
function getProfitableBooksUsed(){
    $q = "SELECT *, 

    ROUND((amazon_used_price/100*80) - lowest_used_price, 2) AS margin 

    FROM ".TBL_BOOKS." 

    WHERE rank < 200000 AND rank IS NOT NULL 
    AND lowest_used_price < amazon_used_price/100*80 
    AND amazon_used_price < 9999 
    AND amazon_used_price < amazon_new_price 
    AND ROUND((amazon_used_price/100*80) - lowest_used_price) > (lowest_used_price/100*".MARGIN.") 

    ORDER BY rank ASC";

    $result = mysql_query($q, $this->connection);

    if(!$result || (mysql_numrows($result) < 1))
        return null;

    $arr = array();
    $numRows = mysql_num_rows($result);//count
    if($numRows>0){
        for($i=0; $i<$numRows; $i++){
            $arr[] = array(
                "isbn"                  => mysql_result($result, $i, "isbn"),
                "title"                 => mysql_result($result, $i, "title"),
                "rank"                  => mysql_result($result, $i, "rank"),                   
                "amazon_price"          => mysql_result($result, $i, "amazon_used_price"),
                "amazon_condition"      => mysql_result($result, $i, "amazon_used_condition"),

                "lowest_price"          => mysql_result($result, $i, "lowest_used_price"),
                "lowest_condition"      => "Used",

                "margin"                => mysql_result($result, $i, "margin"),

                "last_price_updated"    => mysql_result($result, $i, "last_price_updated"),
                "last_rank_updated"     => mysql_result($result, $i, "last_rank_updated")
            );
        }
    }       

    return $arr;
}

join function

/* returns profitable books */
function getProfitableBooks(){
    $new = $this->getProfitableBooksNew();
    $used = $this->getProfitableBooksUsed();

    if($new && $used){
        return array_merge($new, $used);

    }else if($new){
        return $new;

    }else if($used){
        return $used;

    }else{
        return null;
    }
}   

Can anyone give me atleast an idea of how to approach this problem? Thanks

  • 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-09T19:21:55+00:00Added an answer on June 9, 2026 at 7:21 pm

    You could just UNION those two queries into a single query like this

    SELECT *, 
    
        ROUND((amazon_new_price/100*80) - lowest_new_price,2) AS margin,
        'new' AS type 
    
        FROM ".TBL_BOOKS." 
    
        WHERE rank < 200000 AND rank IS NOT NULL 
        AND lowest_new_price < amazon_new_price/100*80 
        AND amazon_new_price < 9999 
        AND ROUND((amazon_new_price/100*80) - lowest_new_price) > (lowest_new_price/100*".MARGIN.") 
    
    UNION
    
    SELECT *, 
    
        ROUND((amazon_used_price/100*80) - lowest_used_price, 2) AS margin,
        'used' as type 
    
        FROM ".TBL_BOOKS." 
    
        WHERE rank < 200000 AND rank IS NOT NULL 
        AND lowest_used_price < amazon_used_price/100*80 
        AND amazon_used_price < 9999 
        AND amazon_used_price < amazon_new_price 
        AND ROUND((amazon_used_price/100*80) - lowest_used_price) > (lowest_used_price/100*".MARGIN.") 
    
    ORDER BY type ASC, rank ASC
    

    Note I have added a field ‘type’ to give you a way to differentiate between the new and used results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble combining audio and video into one file. The Python code looks
I'm having some trouble combining these two specific selectors, whereas I don't have the
I'm having trouble combining a url query parameter rewrite (fancy-url) with a .htaccess ssl
Hi I'm having trouble with my query combining records when it shouldn't. I have
Having trouble with each function... Will try to explain by example... In my code,
I am having trouble compiling a program I have written. I have two different
I am having trouble compiling my widgetset using maven and vaadin. when i run
Following on from a previous question , I am having trouble combining the Lazy<T>
I am having a bit of trouble combining the HOVER and FOCUS events with
I'm having some trouble compiling/linking a set of classes, several of them dealing with

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.