我有以下查询。
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
,而不是 count 作为查询结果。怎么做?
我有以下查询。
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
,而不是 count 作为查询结果。怎么做?
试试这个:
SELECT IF(COUNT(*) > 0, 'true', 'false') AS NewResult
FROM restaurants_offers
WHERE (DATE(NOW()) BETWEEN date_start AND date_end) AND
restaurant_id = 1
SELECT COUNT( * ) > 0 AS has_offers
FROM restaurants_offers
WHERE DATE( NOW( ) )
BETWEEN date_start
AND date_end
AND restaurant_id =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
...