mysql查询字段中的条件判断

简单的值判断用CASE value WHEN compare_value THEN result ELSE result END

复杂的用IF(condition, true_result, false_result)

SELECT
	s.`id` AS '活动ID',
	s.`begin_date` AS '活动开始时间',
	s.`end_date` AS '活动结束时间',
	IF (s.`begin_date` > now() ,'未开始', IF(s.`end_date` < now() , '已结束' , '进行中' )) AS '状态',
	CASE s.`status` WHEN 1 THEN '已上线' ELSE '未上线' END AS '是否可用',
	sr.`product_id` AS '产品ID',
	sr.`sku_id` AS '库存ID'
FROM `activity_range` sr
LEFT JOIN `activity` s ON s.`id` = sr.`activity_id`

发表回复

您的电子邮箱地址不会被公开。