在 MySQL 5.1 中如何将 int 转换为 bit?

发布于 2024-11-27 08:39:19 字数 278 浏览 0 评论 0原文

我正在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

厌味 2024-12-04 08:39:19

你不能!

CAST 和 CONVERT 仅适用于:

  • BINARY[(N)]
  • CHAR[(N)]
  • DATE
  • DATETIME
  • DECIMAL[(M[,D])]
  • SIGNED [INTEGER]
  • TIME
  • UNSIGNED [INTEGER]

没有空间: BIT、BITINT、TINYINT、 MEDIUMINT、BIGINT、SMALLINT,...

但是,您可以创建自己的函数cast_to_bit(n):

DELIMITER $

CREATE FUNCTION cast_to_bit (N INT) RETURNS bit(1)
BEGIN
    RETURN N;
END

要自己尝试,您可以创建视图有几个转换,例如:

CREATE VIEW view_bit AS
    SELECT
        cast_to_bit(0),
        cast_to_bit(1),
        cast_to_bit(FALSE),
        cast_to_bit(TRUE),
        cast_to_bit(b'0'),
        cast_to_bit(b'1'),
        cast_to_bit(2=3),
        cast_to_bit(2=2)

...然后描述它!

DESCRIBE view_bit;

达达!!现在每个人都是位(1)!

You cannot!

CAST and CONVERT only work to:

  • BINARY[(N)]
  • CHAR[(N)]
  • DATE
  • DATETIME
  • DECIMAL[(M[,D])]
  • SIGNED [INTEGER]
  • TIME
  • UNSIGNED [INTEGER]

No room for: BIT, BITINT, TINYINT, MEDIUMINT, BIGINT, SMALLINT, ...

However, you can create your own function cast_to_bit(n):

DELIMITER $

CREATE FUNCTION cast_to_bit (N INT) RETURNS bit(1)
BEGIN
    RETURN N;
END

To try it yourself, you can create view with several conversions like:

CREATE VIEW view_bit AS
    SELECT
        cast_to_bit(0),
        cast_to_bit(1),
        cast_to_bit(FALSE),
        cast_to_bit(TRUE),
        cast_to_bit(b'0'),
        cast_to_bit(b'1'),
        cast_to_bit(2=3),
        cast_to_bit(2=2)

... and then describe it!

DESCRIBE view_bit;

Ta-dah!! Everyone is bit(1) now!!!

忆梦 2024-12-04 08:39:19

尝试 CONV(N,from_base,to_base)

在不同基数之间转换数字。返回数字 N 的字符串表示形式,从基数 from_base 转换为基数 to_base。如果任何参数为 NULL,则返回 NULL。参数 N 被解释为整数,但也可以指定为整数或字符串。最小基数为 2,最大基数为 36。如果 to_base 为负数,则 N 被视为有符号数。否则,N 被视为无符号。 CONV() 以 64 位精度工作。

例如。

select CONV(9, 10, 2);

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.

select CONV(9, 10, 2);
紫南 2024-12-04 08:39:19

尝试使用案例:

CREATE TABLE myNewTable AS
SELECT (case myIntThatIsZeroOrOne when 1 then true else false end) AS myBit
FROM myOldtable

Try using case:

CREATE TABLE myNewTable AS
SELECT (case myIntThatIsZeroOrOne when 1 then true else false end) AS myBit
FROM myOldtable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文