Mysql IF语句或Case

时间:2011-11-21 20:25:33

标签: mysql if-statement case

我正试图获得特殊价值......

if empty p.price + ifnull = option_price_total

else special = option_price_total

SELECT 
  p.product_id,
  p.price,
  (SELECT ifnull(MIN(price), 'empty') AS FIELD_1 FROM etp_product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '1' AND (ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW()) ORDER BY ps.priority, ps.price LIMIT 1) AS special,

/// 如果特别!='空'

这个子查询--- v

  (SELECT ifnull(MIN(price), '0.00') AS FIELD_2 FROM etp_product_option_value pov WHERE pov.product_id = p.product_id) AS option_price,
  p.price + ifnull((SELECT MIN(price) AS FIELD_2 FROM etp_product_option_value pov WHERE pov.product_id = p.product_id GROUP BY p.product_id, p.price), '0.00') AS option_price_total,

/// ELSE

/// ex。特殊值等于15

/// special AS option_price_total

///输出:option_price_total = 15

  (SELECT MIN(etp_product_option_value.price) AS FIELD_1 FROM etp_product_option_value WHERE etp_product_option_value.product_id = p.product_id GROUP BY etp_product_option_value.product_id) AS options,
  (SELECT AVG(rating) AS total FROM etp_review r1 WHERE r1.product_id = p.product_id AND r1.`status` = '1' GROUP BY r1.product_id) AS rating
FROM
  etp_product p
  LEFT OUTER JOIN etp_product_description pd ON (p.product_id = pd.product_id)
  LEFT OUTER JOIN etp_product_to_store p2s ON (p.product_id = p2s.product_id)
  INNER JOIN etp_product_to_vendor ON (p.product_id = etp_product_to_vendor.product_id)
  INNER JOIN etp_vendor ON (etp_product_to_vendor.vendor_id = etp_vendor.vendor_id)

有什么想法吗?

0 个答案:

没有答案