有趣的SQL
```SELECT p
.ProductUrl
, p
.ProductID
, p
.ProductSKU
, p
.ProductName_en
, p
.ProductName_ar
, p
.ProductPrice
, p
.SalePrice
, p
.ProductLive
, p
.ProductLocation
, p
.isSale
, p
.isNew
, p
.isHot
, p
.isRecommend
, p
.AddedBy
, p
.ProductStock
, p
.ProductType
, us
.ip
, cn
.name
as country_name
, group_concat(distinct cp.category_id) as cpProductCatID, group_concat(distinct bp.brand_id) as bpProductBrndID
FROM products
as p
LEFT JOIN brand_products
bp
ON bp
.product_id
=p
.ProductID
LEFT JOIN category_products
cp
ON cp
.product_id
=p
.ProductID
LEFT JOIN product_variation
as pv
ON pv
.productSKU
=p
.productSKU
LEFT JOIN users
as us
ON us
.id
=p
.AddedBy
LEFT JOIN countries
as cn
ON cn
.id
= us
.country_id
WHERE (
p
.ProductLive
= ‘on’
AND p
.isHot
= ‘on’
AND p
.deleted
= 0
)
AND us
.active
= 1
AND p.ProductLive LIKE ‘%on%’ ESCAPE ‘!’
GROUP BY ProductID
ORDER BY p.vendorPromotionOrder = 0, (CASE WHEN p.vendorPromotionOrder > 0 and us.vendorPromotion > 0 THEN p.vendorPromotionOrder END) ASC, (CASE WHEN p.rotation_order > 0 and 1 > 0 THEN p.rotation_order END) ASC, (CASE
WHEN p.ProductStock > 0 and p.ProductType <> ‘custompro’ THEN 2
WHEN p.ProductType = ‘custompro’ THEN 1
WHEN p.ProductStock < 1 THEN 0
ELSE 4
END) DESC, p
.ProductStock
DESC
LIMIT 20
复制