如果确实在上述产品上有折扣,我正在寻找一种快速的方式/方法来使用二级餐桌的折扣价格,否则我想使用第一个餐桌价格。
我用#########标记的那部分是我坚持的地方......我该怎么办?
目前我所拥有的是:
SELECT
cat.`CategoryId` AS CategoryId
,prod.`ProductId` AS ProductId
,COUNT(prod.`ProductId`)AS product_count
,prod.`Price` AS price
,adj.`ProductPrice` AS Adj_Price
FROM ax_category cat
INNER JOIN xx_products prod USING(categoryid)
INNER JOIN xx_subcategory sc ON(prod.Subcategoryid = sc.SubCategoryId)
INNER JOIN xx_services s USING(productid)
LEFT JOIN xx_invoiceline il USING(serviceid)
LEFT JOIN xx_invoices i USING(invoiceid)
#LEFT JOIN xx_adjustments adj ON (s.`ProductId` = adj.`ProductId` AND s.`Quantity` = adj.`Quantity`)
LEFT JOIN xx_adjustments adj ON (s.`ProductId` = adj.`ProductId` AND s.`Quantity` = adj.`Quantity`)
WHERE DATE_FORMAT(s.DateEnd,'%Y-%m') = '2013-11'
############################################
IF EXISTS(
SELECT a.`ProductPrice` AS price
FROM xx_adjustments AS a
WHERE a.`ProductId` = '999'
#limit 1
)
############################################
GROUP BY prod.`ProductId`
答案 0 :(得分:0)
这个怎么样(参见select子句中的discountedPrice列):
SELECT
cat.`CategoryId` AS CategoryId
,prod.`ProductId` AS ProductId
,COUNT(prod.`ProductId`)AS product_count
,prod.`Price` AS price
,adj.`ProductPrice` AS Adj_Price
,IFNULL(adj.`ProductPrice`, prod.`Price`) AS discountedPrice
FROM ax_category cat
INNER JOIN xx_products prod USING(categoryid)
INNER JOIN xx_subcategory sc ON(prod.Subcategoryid = sc.SubCategoryId)
INNER JOIN xx_services s USING(productid)
LEFT JOIN xx_invoiceline il USING(serviceid)
LEFT JOIN xx_invoices i USING(invoiceid)
#LEFT JOIN xx_adjustments adj ON (s.`ProductId` = adj.`ProductId` AND s.`Quantity` = adj.`Quantity`)
LEFT JOIN xx_adjustments adj ON (prod.`ProductId` = adj.`ProductId` AND prod.`Quantity` = adj.`Quantity`)
WHERE DATE_FORMAT(s.DateEnd,'%Y-%m') = '2013-11'
GROUP BY prod.`ProductId`
如果特定productId的xx_adjustment中没有产品价格,请使用xx_products.Price中的价格。
答案 1 :(得分:0)
您可以使用COALESCE()
:
SELECT
cat.`CategoryId` AS CategoryId
,prod.`ProductId` AS ProductId
,COUNT(prod.`ProductId`)AS product_count
,prod.`Price` AS price
,adj.`ProductPrice` AS Adj_Price
,COALESCE(adj.`ProductPrice`, prod.`Price`) AS discountedPrice
FROM ax_category cat
INNER JOIN xx_products prod USING(categoryid)
INNER JOIN xx_subcategory sc ON(prod.Subcategoryid = sc.SubCategoryId)
INNER JOIN xx_services s USING(productid)
LEFT JOIN xx_invoiceline il USING(serviceid)
LEFT JOIN xx_invoices i USING(invoiceid)
#LEFT JOIN xx_adjustments adj ON (s.`ProductId` = adj.`ProductId` AND s.`Quantity` = adj.`Quantity`)
LEFT JOIN xx_adjustments adj ON (s.`ProductId` = adj.`ProductId` AND s.`Quantity` = adj.`Quantity`)
WHERE DATE_FORMAT(s.DateEnd,'%Y-%m') = '2013-11'
GROUP BY prod.`ProductId`
请参阅一个简单示例here。