我有以下查询。
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
,而不是将其作为查询结果。怎么做?
答案 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
或者如果您需要true
和false
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