Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4991

Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4993
Question 85: What is the result of the query? – MySQL Question of the Day

Skip to content

By plogi in mysql questions

select n,n and 3,n & 3, n mod 4, n and false,n and true
from (select 0 n union select 1 union select 2 union
      select 3 union select 4 union select 5 union
      select 6 union select 7 union select null) v1;


 
Hints:
The subquery v1 returns column n with the rows 0,1,2,3,4,5,6,7,null.
“&” is the binary and-operation. “MOD” is the modulo (remainder) calculation.
Mysql treats 0 as false and anything else as true.
The constants TRUE and FALSE have the values 1 and 0.

Tags: , ,

Comment Feed

One Response


  1. select n,n and 3,n & 3, n mod 4, n and false,n and true
    from (select 0 n union select 1 union select 2 union
          select 3 union select 4 union select 5 union
          select 6 union select 7 union select null) v1;
    
    +------+-------+------+-------+-------+-------+
    | n    | n and | n &  | n mod | n and | n and |
    |      | 3     | 3    | 4     | false | true  |
    +------+-------+------+-------+-------+-------+
    |    0 |     0 |    0 |     0 |     0 |     0 |
    |    1 |     1 |    1 |     1 |     0 |     1 |
    |    2 |     1 |    2 |     2 |     0 |     1 |
    |    3 |     1 |    3 |     3 |     0 |     1 |
    |    4 |     1 |    0 |     0 |     0 |     1 |
    |    5 |     1 |    1 |     1 |     0 |     1 |
    |    6 |     1 |    2 |     2 |     0 |     1 |
    |    7 |     1 |    3 |     3 |     0 |     1 |
    | NULL |  NULL | NULL |  NULL |     0 |  NULL |
    +------+-------+------+-------+-------+-------+
    9 rows in set (0.00 sec)
    


    And, did you get it right?
    “3″ is a true value, that makes “n and true” and “n and 3″ the same. The modulo and bit-operations are obvious and so is the fact, that an operation with NULL results in NULL.
    For me the most interesting part is, that TRUE AND NULL is NULL, where as FALSE AND NULL is FALSE.

You must be logged in to post a comment.