在 MySQL 5.1 中如何将 int 转换为 bit?
我正在从 SQL Server 过渡到 MySQL 5.1,并且似乎在尝试使用 select 语句创建表时遇到了麻烦,因此该列有点。
理想情况下,以下内容可以工作:
CREATE TABLE myNewTable AS
SELECT cast(myIntThatIsZeroOrOne as bit) AS myBit
FROM myOldtable
但是 sql 对强制转换非常不满意。我怎样才能告诉它选择一个 int 列(我知道只有 0 和 1)作为位?
I am transitioning from SQL Server to MySQL 5.1 and seem to be tripped up trying to create a table using a select statement so that the column is a bit.
Ideally the following would work:
CREATE TABLE myNewTable AS
SELECT cast(myIntThatIsZeroOrOne as bit) AS myBit
FROM myOldtable
However sql is very unhappy at casting as a bit. How can I tell it to select an int column (which I know only has 0s and 1s) as a bit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不能!
CAST 和 CONVERT 仅适用于:
没有空间: BIT、BITINT、TINYINT、 MEDIUMINT、BIGINT、SMALLINT,...
但是,您可以创建自己的函数cast_to_bit(n):
要自己尝试,您可以创建视图有几个转换,例如:
...然后描述它!
达达!!现在每个人都是位(1)!
You cannot!
CAST and CONVERT only work to:
No room for: BIT, BITINT, TINYINT, MEDIUMINT, BIGINT, SMALLINT, ...
However, you can create your own function cast_to_bit(n):
To try it yourself, you can create view with several conversions like:
... and then describe it!
Ta-dah!! Everyone is bit(1) now!!!
尝试 CONV(N,from_base,to_base)
在不同基数之间转换数字。返回数字 N 的字符串表示形式,从基数 from_base 转换为基数 to_base。如果任何参数为 NULL,则返回 NULL。参数 N 被解释为整数,但也可以指定为整数或字符串。最小基数为 2,最大基数为 36。如果 to_base 为负数,则 N 被视为有符号数。否则,N 被视为无符号。
CONV()
以 64 位精度工作。例如。
Try
CONV(N,from_base,to_base)
Converts numbers between different number bases. Returns a string representation of the number N, converted from base from_base to base to_base. Returns NULL if any argument is NULL. The argument N is interpreted as an integer, but may be specified as an integer or a string. The minimum base is 2 and the maximum base is 36. If to_base is a negative number, N is regarded as a signed number. Otherwise, N is treated as unsigned.
CONV()
works with 64-bit precision.for example.
尝试使用案例:
Try using case: