[英]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.