2016-08-24 56 views
0

我有一个产品price 2列表示正常价格和new_price代表产品的报价。当我按价格订购时,如果两列都填满了,我无法得到正确的订单。现在它先以新价格然后按价格订购。我想以某种方式连接价格。Mysql通过2列表示价格和新价格

if($order==1) 
{ 
    $order_by='Produse.New_price ASC,Produse.Price ASC'; 
} 

if($order==2) 
{ 
    $order_by='Produse.New_Price DESC,Produse.Price DESC'; 
}  

if($order==3) 
{ 
    $order_by='Produse.Nume_Prod ASC'; 
} 

if($order==4) 
{ 
    $order_by='Produse.Nume_Prod DESC'; 
} 

if($order==0) 
{ 
    $order_by='Produse.Nume_Prod DESC'; 
} 
     $stmt=$dbh->prepare("Select * FROM Produse 
           INNER JOIN Categorii on Produse.ID_Categorie=Categorii.ID_Categorie 
          where Categorii.Nume_Categ=:id 
          ORDER BY $order_by"); 
     $stmt->bindParam(':id',$id,PDO::PARAM_INT); 
     $stmt->execute(); 
+0

使用COALESCE返回在新的价格的第一个非空价格vs price ...'通过合并订购('Produse.new_price,produse.price)'这个假设当两者都存在时,您想以新价格订购。并且新价格为空,您只需按价格订购。 – xQbert

+0

取决于你想要的东西......'order by new_price + price DESC'' by order by IF(new_price IS NOT NULL,new_price,price)DESC'或者简单地按一列排序,因为对我来说它根本没有意义;) – iRaS

+0

“以某种方式连接价格”是什么意思? –

回答

1

使用通过合并新价格进行排序,如果存在(不为空),然后通过价格,如果新的价格是空...

if($order==1) 
{ 
    $order_by='coalesce(Produse.New_price,Produse.Price) ASC'; 
} 
if($order==2) 
{ 
    $order_by='Coalesce(Produse.New_Price,Produse.Price) DESC'; 
}  
if($order==3) 
{ 
    $order_by='Produse.Nume_Prod ASC'; 
} 
if($order==4) 
{ 
    $order_by='Produse.Nume_Prod DESC'; 
} 
if($order==0) 
{ 
    $order_by='Produse.Nume_Prod DESC'; 
} 
     $stmt=$dbh->prepare("Select * FROM Produse 
           INNER JOIN Categorii on Produse.ID_Categorie=Categorii.ID_Categorie 
          where Categorii.Nume_Categ=:id 
          ORDER BY $order_by"); 
     $stmt->bindParam(':id',$id,PDO::PARAM_INT); 
     $stmt->execute(); 
+0

thnx,它的工作:D – chris227