mysql中binary相加的问题

WhiteBallon 2011-04-11 11:28:30
我在mssql中有这样一段代码
declare @byte1 binary(1)
declare @byte2 binary(1)
declare @smallint smallint
set @smallint = 675
set @byte1 = @smallint & 0xFF
set @byte2 = (@smallint & 0xFF00) / 0x100

select @byte1 a, @byte2 b, @byte1+@byte2 c


执行结果是:
a b c
0xA3 0x02 0xA302

在mysql中我该怎么做才能得到正确结果呢?
...全文
199 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
WhiteBallon 2011-04-27
  • 打赏
  • 举报
回复
CREATE DEFINER=`root`@`localhost` FUNCTION `F_TEST`(	iPos tinyint
,nID int
,nLv tinyint
,nOtherID int
,iRate int) RETURNS longblob
BEGIN
return char(iPos&0xFF,

nID&0xFF,
(nID&0xFF00) / 0x100,

nLv&0xFF,

nOtherID&0xFF,
(nOtherID&0xFF00) / 0x100,

iRate&0xFF,
(iRate&0xFF00) / 0x100
);
END
WhiteBallon 2011-04-27
  • 打赏
  • 举报
回复
最后使用longblob 来代替原来在sqlserver中的varbinary.
使用char()函数来生成longblob数据
ACMAIN_CHM 2011-04-11
  • 打赏
  • 举报
回复
MYSQL中字符串相连不是 str1+str2 ,这个是算术加, 字符串是 concat(str1,str2)

另外BINARY在MYSQL中与MS SQL也不相同。binary(2)显然无法存储 字符串 a302

MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html
WhiteBallon 2011-04-11
  • 打赏
  • 举报
回复
我是在把mssql函数转到mysql时遇到的问题,mssql中的函数已经贴出。
在mysql中我这样写:
CREATE  FUNCTION `Sint16ToBinary2`(small_int smallint) RETURNS binary(2)
BEGIN
declare byte1 binary(1);
declare byte2 binary(1);
set @byte1 = small_int & 0xFF;
set @byte2 = (small_int & 0xFF00) / 0x100;
RETURN ( @byte1 + @byte2) ;
END


显然这里有错误,如下
mysql> set @a = Sint16ToBinary2(675);
1406 - Data too long for column 'Sint16ToBinary2' at row 1

我想得到的结果,以675为例
set @byte1 = small_int & 0xFF; # 结果a3
set @byte2 = (small_int & 0xFF00) / 0x100; # 结果2

需要得到a302,而不是 a3+2=a5.

ACMAIN_CHM 2011-04-11
  • 打赏
  • 举报
回复
直接写出你自己的MYSQL代码,包括你期望的正确结果是什么样?

BINARY本身就可以直接AND啊。但相加的话,则应该直接用+


问题说明越详细,回答也会越准确!参见如何提问。(提问的智慧
WhiteBallon 2011-04-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mysqldbd 的回复:]
[/Quote]

我是想问怎样把它转换为mysql :)
mysqldbd 2011-04-11
  • 打赏
  • 举报
回复
[Quote=引用楼主 foozuu 的回复:]
我在mssql中有这样一段代码

SQL code
declare @byte1 binary(1)
declare @byte2 binary(1)
declare @smallint smallint
set @smallint = 675
set @byte1 = @smallint & 0xFF
set @byte2 = (@smallint & 0xF……
[/Quote]


这里是mysql论坛,去mssql论坛吧。
rucypli 2011-04-11
  • 打赏
  • 举报
回复
mysql
BINARY和VARBINARY类类似于CHAR和VARCHAR,不同的是它们包含二进制字符串而不要非二进制字符串。也就是说,它们包含字节字符串而不是字符字符串。这说明它们没有字符集,并且排序和比较基于列值字节的数值值。

sqlserver
binary(n)
固定长度的二进制数据,最大长度为 8000 字节。默认长度 = 1。

存储大小是固定的,是在类型中声明的以字节为单位的长度。

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧