mysql - 当count大于零时选择true

时间:2012-08-13 09:11:09

标签: mysql

我有以下查询。

SELECT COUNT( * ) AS offer_count
  FROM restaurants_offers
WHERE DATE( NOW( ) ) 
   BETWEEN date_start
   AND date_end
AND restaurant_id =1

现在,当count大于零时,我想选择true else false,而不是将其作为查询结果。怎么做?

3 个答案:

答案 0 :(得分:15)

试试这个:

SELECT IF(COUNT(*) > 0, 'true', 'false') AS NewResult
  FROM restaurants_offers
 WHERE (DATE(NOW())  BETWEEN date_start AND date_end) AND 
       restaurant_id = 1

答案 1 :(得分:1)

SELECT case when COUNT(*) > 0 
            then 1
            else 0
       end AS offer_count
FROM restaurants_offers
WHERE DATE( NOW( ) ) 
BETWEEN date_start
AND date_end
AND restaurant_id =1

或者如果您需要truefalse

这两个字词
SELECT case when COUNT(*) > 0 
            then 'true'
            else 'false'
       end AS offer_count
...

答案 2 :(得分:1)

SELECT COUNT( * ) > 0 AS has_offers
  FROM restaurants_offers
WHERE DATE( NOW( ) ) 
   BETWEEN date_start
   AND date_end
AND restaurant_id =1