Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL always returning BIT values as blank

From my create table script, I've defined the hasMultipleColors field as a BIT:

hasMultipleColors BIT NOT NULL,

When running an INSERT, there are no warnings thrown for this or the other BIT fields, but selecting the rows shows that all BIT values are blank.

Manually trying to UPDATE these records from the command line gives odd effect - shows that the record was match and changed (if appropriate), but still always shows blank.

Server version: 5.5.24-0ubuntu0.12.04.1 (Ubuntu)

mysql> update pumps set hasMultipleColors = 1 where id = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql> select hasMultipleColors from pumps where id = 1;
+-------------------+
| hasMultipleColors |
+-------------------+
|                  |
+-------------------+
1 row in set (0.00 sec)

mysql> update pumps set hasMultipleColors = b'0' where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select hasMultipleColors from pumps where id = 1;
+-------------------+
| hasMultipleColors |
+-------------------+
|                   |
+-------------------+
1 row in set (0.00 sec)

Any thoughts?

like image 721
CdrXndr Avatar asked Jul 23 '12 09:07

CdrXndr


People also ask

How do I stop null values in MySQL?

Example - With SELECT Statement Here is an example of how to use the MySQL IS NOT NULL condition in a SELECT statement: SELECT * FROM contacts WHERE last_name IS NOT NULL; This MySQL IS NOT NULL example will return all records from the contacts table where the last_name does not contain a null value.

Is bit datatype in MySQL?

BIT is a data type used in MySQL that allows us to store bit values. The bit value comes in a range of 1-64. It will store values only in 0 and 1. If we store a bit value like 2, it will return an error message.

What is bit value in MySQL?

The BIT data type is used to store bit values. A type of BIT( M ) enables storage of M -bit values. M can range from 1 to 64. To specify bit values, b' value ' notation can be used. value is a binary value written using zeros and ones.

Is null and 0 the same in MySQL?

In MySQL, a NULL value means unknown. A NULL value is different from zero ( 0 ) or an empty string '' . A NULL value is not equal to anything, even itself. If you compare a NULL value with another NULL value or any other value, the result is NULL because the value of each NULL value is unknown.


3 Answers

You need to cast the bit field to an integer.

mysql> select hasMultipleColors+0 from pumps where id = 1;

This is because of a bug, see: http://bugs.mysql.com/bug.php?id=43670. The status says: Won't fix.

like image 60
RTB Avatar answered Oct 13 '22 09:10

RTB


You can cast BIT field to unsigned.

  SELECT CAST(hasMultipleColors AS UNSIGNED) AS hasMultipleColors 
  FROM pumps 
  WHERE id = 1

It will return 1 or 0 based on the value of hasMultipleColors.

like image 32
A J Avatar answered Oct 13 '22 11:10

A J


You need to perform a conversion as bit 1 is not printable.

SELECT hasMultipleColors+0 from pumps where id = 1;

See more here: http://dev.mysql.com/doc/refman/5.0/en/bit-field-literals.html

like image 6
mihaisimi Avatar answered Oct 13 '22 10:10

mihaisimi