7

I'm baffled as to what this SQL statement means:

SELECT exhibitor_categories+0 from exhibitor_registry

What is exhibitor_categories+0 ? It returns a number for each row returned.

exhibitor_categories is defined as:

set('contemporary', 'classical impression / transitional', 'outdoor', 'home accessories')

Thanks for your time :)

1
  • +1 Nice question. I wonder if there is an equivalent for SQL Server 200/2005?
    – J.Hendrix
    Commented Nov 9, 2009 at 17:35

2 Answers 2

7

It implicity converts the set value to an INTEGER.

A set is treated as a bitmap, so the first value sets bit 0, the second value sets bit 1 etc.

mysql> CREATE TABLE exhibitor_registry(exhibitor_categories set('contemporary',
'classical impression / transitional', 'outdoor', 'home accessories') NOT NULL);

Query OK, 0 rows affected (0.08 sec)

mysql> INSERT
    -> INTO    exhibitor_registry
    -> VALUES  ('contemporary,classical impression / transitional,outdoor');
Query OK, 1 row affected (0.03 sec)

mysql> SELECT  exhibitor_categories
    -> FROM    exhibitor_registry;
+----------------------------------------------------------+
| exhibitor_categories                                     |
+----------------------------------------------------------+
| contemporary,classical impression / transitional,outdoor |
+----------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT  exhibitor_categories + 0
    -> FROM    exhibitor_registry;
+--------------------------+
| exhibitor_categories + 0 |
+--------------------------+
|                        7 |
+--------------------------+
1 row in set (0.00 sec)
3
  • for example, how is a value like "contemporary" converted into an integer? or, since it's a set, how is "contemporary,outdoor" converted into an integer? do you have any insights as to the purpose of converting a set value into an integer?
    – Obay
    Commented Nov 9, 2009 at 17:28
  • wow! very helpful! how did it become 7 though? wouldn't it be 6 because 1+2+3 = 6 ?
    – Obay
    Commented Nov 9, 2009 at 17:41
  • 1
    @Obay: a bitmap adds powers of two so they don't get mixed. It's 2^0 + 2^1 + 2^2 = 1 + 2 + 4 = 7. With 6, you wouldn't be able to distinguish between, say, 1 + 2 + 3 and 2 + 4.
    – Quassnoi
    Commented Nov 9, 2009 at 17:47
4

Check out http://dev.mysql.com/doc/refman/5.0/en/set.html for the full skinny, but basically a field like that is known as an 'set' - it has a list of possible values, which can only be one or more of those. The value is stored as a number... which means the exhibitor_categories is actually storing the value 4 when someone sets the value to 'outdoor', because it's setting the third bit - '0100'. When you get the value back out of the database later, mysql automatically turns '0100' back into 'outdoor' for you.

But, by adding +0 to the query, you force the result to stay a number, so you would actually get the number value '0100' if the row's value was set to 'outdoor' in this case.

Apologies for getting enum and set mixed up.

Why, you might ask, is it setting the value to '0100' instead of just saying '3', like in an enum? Because a set can hold multiple values - if the values 'contemporary' (0001) and 'outdoor' (0100) were selected, it would store '0101' <- setting the 1st and 3rd bits, which would be returned as '5' if you use the +0 code.

2
  • 1
    This is a set (bitmap), not an enum. Storing outdoor will set the numerical value to 4.
    – Quassnoi
    Commented Nov 9, 2009 at 17:35
  • Quassnoi, why 4? Wouldn't it be 3 since it's the third element declared?
    – Obay
    Commented Nov 9, 2009 at 17:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.