MySQL两个选择但完全不同

时间:2012-02-09 00:21:49

标签: php mysql select

好吧,我必须使用相同的表格,但列出所有销售商品的商店和一个我们销售的商品。

想想它与水果和蔬菜完全不同。

我需要解决的是,如果有7个水果,我们需要8个列表,然后去获得一个随机的蔬菜,并在相同的结果中显示它。

以下是我们的查询目前的样子。你会注意到我们可以发送一个我们发送的$计数为8,但我们可能希望增加到10甚至是4。

public function realcashoffers($state,$count)
{
    $this->state = $state;
    $this->number = $count;
    //print count($this->JSONselect("business_stores","*",NULL,NULL),1);
        print $this->JSONselect("approved_business, business_stores, Real_Cash_Offers"," *, group_concat(offer ORDER BY offer ASC SEPARATOR ',') as offers"," approved_business.id = business_stores.business_id AND  Real_Cash_Offers.business_id = approved_business.id  AND Real_Cash_Offers.storeid = business_stores.storeid AND business_stores.state = '{$this->state}'","GROUP BY id ORDER BY RAND(), approved_business.id DESC LIMIT {$this->number} ");
}

this-> JSONselect转到

//JSON select
    public function JSONselect($table,$options,$where,$orderby)
    {
        $options = empty($options) ? "*"   : $options;
        $where  =  empty($where)   ? "1=1" : $where;
        $orderby = empty($orderby) ? ""    : $orderby;

        $qry = "SELECT $options FROM $table WHERE $where $orderby ";
        //print $qry;
        $result = mysql_query($qry) or die(json_encode(array("error",mysql_error())));

        while(($row = mysql_fetch_assoc($result))){ $resultArray[] = $row; }

        //print json_encode($resultArray);

        return count($resultArray) < 1 ? print "[".json_encode(array("error"=>"sorry"))."]" : json_encode($resultArray);
    }

1 个答案:

答案 0 :(得分:0)

如果我理解正确,我认为你所寻找的是与此类似的东西;

更新主要功能以确定是否有足够的结果,如果不是

则调用辅助查询
public function realcashoffers($state,$count)
{
     $this->state = $state;
     $this->number = $count;         
     $result = $this->JSONselect("approved_business, business_stores, Real_Cash_Offers"," *, group_concat(offer ORDER BY offer ASC SEPARATOR ',') as offers"," approved_business.id = business_stores.business_id AND  Real_Cash_Offers.business_id = approved_business.id  AND Real_Cash_Offers.storeid = business_stores.storeid AND business_stores.state = '{$this->state}'","GROUP BY id ORDER BY RAND(), approved_business.id DESC LIMIT {$this->number} ");

     $remaining = count($result) - $count;

     if ($remaining) {
        $result = array_merge($result, $this->JSONselect(.. enter secondary call here using $remaining as the limit..);

     }

     $this->JSONprint($result);
}

更新JSONselect以返回结果,而不是负责打印它们

public function JSONselect($table,$options,$where,$orderby)
{
    $resultArray = array();
    $options = empty($options) ? "*"   : $options;
    $where  =  empty($where)   ? "1=1" : $where;
    $orderby = empty($orderby) ? ""    : $orderby;

    $qry = "SELECT $options FROM $table WHERE $where $orderby ";
    //print $qry;
    $result = mysql_query($qry) or die(json_encode(array("error",mysql_error())));

    while(($row = mysql_fetch_assoc($result))){ $resultArray[] = $row; }

    //print json_encode($resultArray);

    return $resultArray;
}

创建将打印返回结果的JSONprint

protected function JSONprint($resultArray) {
    return count($resultArray) < 1 ? print "[".json_encode(array("error"=>"sorry"))."]" : json_encode($resultArray);
}