13

次のクエリがあります。

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

ここで、 count がゼロより大きい場合、クエリ結果として count ではなく、trueelseを選択します。falseどうやってするか?

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

trueまたは、言葉が必要な場合はfalse

SELECT case when COUNT(*) > 0 
            then 'true'
            else 'false'
       end AS offer_count
...
于 2012-08-13T09:12:43.840 に答える