I am not so into SQL and I have the following doubt. I have this query (I am using MySql):
SELECT count(*) AS exist
FROM MeteoForecast AS MF
WHERE
MF.localization_id = 22
AND
DATE(MF.start_date) = DATE('2019/03/02')
returning the number of record into the MeteoForecast table as the exist field.
I want to modify it in this way:
if the value of the count(*) is equal to 2, it returns true (the exist field is true), otherwise the exist field value is false.
How can I implement this behavior?
You can just use a boolean expression:
SELECT (count(*) = 2) AS exist2
FROM MeteoForecast mf
WHERE MF.localization_id = 22 AND
DATE(MF.start_date) = DATE('2019-03-02') ;
This expression actually returns true as "1" and false as "0". Those are the numeric equivalents of the booleans true and false in MySQL.
Assuming that start_date
has a time component, you should write this as:
SELECT (count(*) = 2) AS exist2
FROM MeteoForecast mf
WHERE MF.localization_id = 22 AND
MF.start_date >= '2019-03-02' AND
MF.stara_date < '2019-03-03' ;
This can take advantage of an index on MeteoForecast(localization_id, start_date)
, so it can be much faster.