简体   繁体   English

如何将两个类似的MySQL语句合并为一个

[英]How to combine two similiar MySQL statements into one

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. 我无法将两个MySQL语句合并为一个,目前我必须运行它们然后将它们放入一个数组然后合并这些数组。

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 谢谢

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. 注意我添加了一个字段'type',以便您区分新结果和使用结果。

You can use UNION syntax 您可以使用UNION语法

$sql = "(SELECT a FROM t1 WHERE a=10 AND B=1) 
UNION 
(SELECT a FROM t2 WHERE a=11 AND B=2) ORDER BY a LIMIT 10;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM