0

In my inherited database, sometimes binary questions are encoded as ('Yes', 'No'), and sometimes as (1,0). I mistakenly queried against the string 'Yes' on a field which was numerically encoded. My guess is that the string was turned into a '0' by MySQL.

I have a query like this:

SELECT `children_under_18`
  FROM `households`
 WHERE `children_under_18` = 'Yes'
 GROUP BY `children_under_18`

It ended up only matching records where children_under_18 was 0, the opposite of what I wanted. I know I need to be more careful. I am looking for a definitive answer as to what happened.

2
  • 2
    what is the type of children_under_18 Commented Nov 21, 2013 at 0:38
  • Jorge: children_under_18 is an int type. Usually the database designer chose to encode this type of value as enum('Yes', 'No'). I forgot what the encoding was for this field, which is how I obtained undesired results. Commented Nov 21, 2013 at 15:59

1 Answer 1

1

String is always converted to 0 when compared to numeric (of course string containing numeric + string is converted numeric. but this is not good practice)

mysql> SELECT 'Yes' + 0;
+-----------+
| 'Yes' + 0 |
+-----------+
|         0 |
+-----------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+-----------------------------------------+
| Level   | Code | Message                                 |
+---------+------+-----------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'Yes' |
+---------+------+-----------------------------------------+

If you want store binary value (Yes/No or True/False) or small value set, ENUM is good choice. It takes small disk space and can use meaningful String.

mysql> CREATE TABLE enum_test(a ENUM('Yes', 'No'));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into enum_test values('Yes'), ('No'), ('Invalid');
Query OK, 3 rows affected, 1 warning (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 1

mysql> show warnings;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1265 | Data truncated for column 'a' at row 3 |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)

mysql> select * from enum_test where a = 'Yes';
+------+
| a    |
+------+
| Yes  |
+------+
1 row in set (0.00 sec)

mysql> select * from enum_test where a = 'No';
+------+
| a    |
+------+
| No   |
+------+
1 row in set (0.00 sec)
Sign up to request clarification or add additional context in comments.

2 Comments

For a simple boolean, it's arguable that ENUM may be appropriate (although the truncation of invalid values to the special empty-string value '' as demonstrated above, in addition to explicit NULL values if so permitted, may suggest otherwise). However, one nevertheless ought to read Chris Komlenic's article 8 Reasons Why MySQL's ENUM Data Type Is Evil before deploying it in a production system.
@eggyal Great post it is! actually I hadn't used ENUM I'm just lazy, but int the future never use it, even if be industrious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.