我有以下 MySQL 查询:
SELECT
*
FROM
product
INNER JOIN productcontent ON product.id = productcontent.productID
WHERE
product.deactivate <> '1'
AND productcontent.price > 0 { $level1SQL }
AND menulevel IN ($imploded)
OR menulevel = '$menulevelID' { $level1SQL }
ORDER BY
(
CASE
WHEN discountPrice < price
AND discountPrice > 0 THEN
CAST(
discountPrice AS DECIMAL (10, 2)
)
ELSE
CAST(
price AS DECIMAL (10, 2)
)
END
) { $price_order }
LIMIT { $limit }
但我还需要确保,如果 product.notInStock
设置为 1
,则在最后订购(但仍按名称/价格,如果选择这样的排序),是否有可能,或者我应该只使用 2 个查询 - 一个用于有库存的产品,一个用于没有库存的产品?
最佳答案
您可以向 ORDER BY
添加一个附加键:
SELECT *
FROM product p INNER JOIN
productcontent pc
ON p.id = pc.productID
WHERE p.deactivate <> '1' AND
pc.price > 0 {$level1SQL} AND
(menulevel IN ($imploded) OR menulevel = '$menulevelID')
{$level1SQL}
ORDER BY p.not_inStock DESC,
(case when discountPrice < price and discountPrice > 0
then CAST(discountPrice AS DECIMAL(10,2))
else CAST(price AS DECIMAL(10,2))
end) {$price_order}
LIMIT {$limit}
此外,menulevel
上的最后一个条件表明您需要括号。
关于mysql - 如何最后订购没有库存的商品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38765291/