Last answered:

18 Nov 2022

Posted on:

25 Sept 2022

0

Resolved: Question on Logical Operators

Hello,

In the lecture, we use the condition below inside the CASE statement:

YEAR(dm.to_date) >= e.calendar_year AND YEAR(dm.from_date) <= e.calendar_year



But as far as I know, that statement is logically equivalent to:

YEAR(dm.from_date) <= e.calendar_year <= YEAR(dm.to_date)

which I used in my query when trying to do it by myself. However, my active field return all 1's and no 0's.

Does that mean that MySQL take the two logical statements differently? Why is that?

Thanks,
Carl

3 answers ( 3 marked as helpful)
Instructor
Posted on:

30 Sept 2022

1

Hi Carl!

Thanks for reaching out.

Indeed, what you are referring to can be achieved by using a combination between a WHERE and BETWEEN condition:

...
WHERE field_name BETWEEN from_date AND to_date;

Please note that MySQL's BETWEEN clause includes the two values provided (here these being from_date and to_date).

Otherwise, we advise using the structure we've provided, perhaps writing every next condition (as separated by the AND keyword) on a new line:

...
WHERE
            YEAR(dm.to_date) >= e.calendar_year
            AND YEAR(dm.from_date) <= e.calendar_year
            AND...

Hope this helps.
Best,
Martin

Posted on:

18 Nov 2022

0

we can even write
"  CASE
        WHEN  e.calendar_year BETWEEN YEAR(dm.to_date) AND YEAR(dm.from_date) THEN 1
        ELSE 0 "
If you want to use BEWTEEN AND
It works for me

Posted on:

18 Nov 2022

0

It's not that I don't want to use the methods mentioned above. I'm just exploring ways to write clean code, and I thought that if its mathematically logical, then it should also work, but apparently there's some limitations to that.

Submit an answer