我有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();
答案 0 :(得分:1)
如果新价格为空,则使用Coalesce按新价格排序(非空),然后按价格排序......
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();