13

我有以下查询。

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

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

4

3 回答 3

28

试试这个:

SELECT IF(COUNT(*) > 0, 'true', 'false') AS NewResult
  FROM restaurants_offers
 WHERE (DATE(NOW())  BETWEEN date_start AND date_end) AND 
       restaurant_id = 1
于 2012-08-13T09:12:55.470 回答
3
SELECT COUNT( * ) > 0 AS has_offers
  FROM restaurants_offers
WHERE DATE( NOW( ) ) 
   BETWEEN date_start
   AND date_end
AND restaurant_id =1
于 2015-09-24T13:37:37.683 回答
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
...
于 2012-08-13T09:12:43.840 回答