繁体   English   中英

MySQL按2列表示价格和新价格

[英]Mysql order by 2 columns that represent price and new price

我有2个列,分别代表正常价格的产品price和代表产品报价的new_price 当我按价格订购它们时,如果两栏都填写,我将无法获得正确的订单。 现在,它首先按新价格订购,然后按价格订购。 我想以某种方式连接价格。

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();

使用Coalesce按新价格(如果不存在)排序(不为null),然后按新价格为null的价格排序...

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();

暂无
暂无

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

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