问题

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?

回答1:

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.

回答2:

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.

回答3:

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

回答4:

The actual reason for the effect you see, is that it's done right and as expected.

The bit field has bits and thus return bits, and trying to output a single bit as a character will show the character with the given bit-value – in this case a zero-width control character.

Some software may handle this automagically, but for command line MySQL you'll have to cast it as int in some way (e.g. by adding zero).

In languages like PHP the ordinal value of the character will give you the right value, using the ord() function (though to be really proper, it would have to be converted from decimal to binary string, to work for bit fields longer than one character).

EDIT:

Found a quite old source saying that it changed, so a MySQL upgrade might make everything work more as expected: http://gphemsley.wordpress.com/2010/02/08/php-mysql-and-the-bit-field-type/

来源:https://stackoverflow.com/questions/11609474/mysql-always-returning-bit-values-as-blank

mysql always as_MySQL always returning BIT values as blank相关推荐

  1. Mysql插入语句之value与values区别

    value与values区别 http://dev.mysql.com/doc/refman/5.7/en/insert.html 看了一下官方文档,VALUE与VALUES都是正确的,经过验证,这两 ...

  2. mysql中插入数据value与values的区别

    value与values区别 本文来自 予怀之言 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq_26683009/article/details/5252683 ...

  3. mysql with as_mysql数据库学习(第十六篇)- 视图

    视图 视图的行和列来自定义视图的查询中使用的表,并且是在使用视图时动态生成的. 创建视图 Create view 视图名字 as ( 单表 or 多表 or 视图) Create / Replace ...

  4. mysql 字段 as_mysql 字段as详解及实例代码

    mysql 字段使用as 在mysql中,select查询可以使用AS关键字为查询的字段起一个别名,该别名用作表达式的列名,并且别名可以在GROUP BY,ORDER BY或HAVING等语句中使用. ...

  5. mysql table as_Mysql中create table as与like命令的区别

    MySQL 是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性.MySQL 所使用的 SQL 语言是用于访问数据库的最常用标准 ...

  6. mysql cast as_Mysql SQL CAST()函数

    (1).CAST()函数的参数是一个表达式,它包括用AS关键字分隔的源值和目标数据类型.以下例子用于将文本字符串'12'转换为整型: SELECT CAST('12' AS int) (2).返回值是 ...

  7. mysql pam 配置_pam_mysql 安装配置总结 (结合vsftpd)

    前一段时间,论坛里总有朋友问到pam-mysql的安装问题.比较典型的有: vsftp1.2+mysql4.1+pam_mysql0.5在RedHat AS4(32bit)下好像是有bug 还是mys ...

  8. mysql nodejs begin_nodejs中mysql用法

    nodejs也算是一篇脚本了我们来看nodejs如何使用mysql数据库了有了它们两组合感觉还是非常的不错哦,下面一起来看nodejs中使用mysql数据库的示例,希望能够帮助到各位. db-mysq ...

  9. mysql 指南_MySQL-快速指南

    mysql 指南 MySQL-快速指南 (MySQL - Quick Guide) MySQL-简介 (MySQL - Introduction) 什么是数据库? (What is a Databas ...

最新文章

  1. 美国光伏发电市场是否稳定?电池板价格降至40美分/W!
  2. EXT GridPanel
  3. Django-发送注册、忘记密码邮件验证-send_mail
  4. 一个线程资源中有多个模态对话框的问题
  5. 美国将尝试区块链领域和加密货币相结合
  6. 马斯克:大量特斯拉汽车将会在年底前交付
  7. L1-021 重要的话说三遍 (5 分) — 团体程序设计天梯赛
  8. bat脚本 拷贝文件/文件夹到目标目录
  9. 【BZOJ】【4010】【HNOI2015】菜肴制作
  10. Node.js 0.8.20 稳定版发布
  11. M3U8下载,直播源下载,FLASH下载(四)-m3u8直播源下载工具类
  12. 玩转A2ZS,软改A4
  13. java初级项目 小说_webmagic项目实战(爬小说网站)
  14. 计算机的硬盘配额如何更改,磁盘配额怎么设置
  15. java-learning:32. ArrayList类应用
  16. QT 如何计算中英文字符串的长度
  17. 强制应用横竖屏的简单设置
  18. 基于JAVA疫情物资商城和疫情数据可视化系统设计与实现 开题报告
  19. JAVA简单大数运算
  20. JAVAWeb学习笔记2020/5/15——JavaScript高级笔记

热门文章

  1. vSphere 7简介:混合云的功能和技术
  2. vmware esxi主机经常出现的警示“vsphere distributed switch mtu支持状态, vlan中继状态”求大神回复。
  3. VCSA6.7 备份和还原
  4. Mysql学习总结(54)——MySQL 集群常用的几种高可用架构方案
  5. Spring MVC学习总结(12)——Spring MVC集成Swagger时报错{schemaValidationMessages:[
  6. Spring学习总结(19)——Spring概念详解
  7. Oracle学习总结(7)—— 常用的数据库索引优化语句总结
  8. 华为杯数学建模优秀论文_数学建模经典例题(2013年国赛A题与优秀论文)
  9. MVP公益活动:编程一小时
  10. iOS-OC-APP热更新,动态更新(仿QQ打开或关闭某个功能)