robert
robert

Reputation: 8717

mysql - select true when count is greater than zero

I have the following query.

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

Now, when count is greater than zero I want to select true else false, instead of count as the query result. How to do that?

Upvotes: 15

Views: 30233

Answers (3)

orasio
orasio

Reputation: 31

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

Upvotes: 3

John Woo
John Woo

Reputation: 263713

Try this:

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

Upvotes: 30

juergen d
juergen d

Reputation: 204756

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

or if you need the words trueand false

SELECT case when COUNT(*) > 0 
            then 'true'
            else 'false'
       end AS offer_count
...

Upvotes: 1

Related Questions