简体   繁体   English

Mysql select from mediumint(8) to unique Binary(2)

[英]Mysql select from mediumint(8) to unique Binary(2)

I am trying to create a query to populate unique data from one table into part of another table.我正在尝试创建一个查询以将一个表中的唯一数据填充到另一个表的一部分中。 The two rows from the source table I'm selecting are of types mediumint(8) and varchar(20).我选择的源表中的两行是 mediumint(8) 和 varchar(20) 类型。 In the destination table I have a Binary(2) and a varchar(20) that I'm trying to populate from the source table.在目标表中,我有一个 Binary(2) 和一个 varchar(20),我试图从源表中填充它们。 I'm also only selecting the top 100 unique values.我也只选择前 100 个唯一值。

The problem I have is with the mediumint(8) to Binary(2) conversion.我遇到的问题是 mediumint(8) 到 Binary(2) 的转换。 I can see all my mediumint(8) values are unique, and they are all less than 200 numerically, but for some reason I can't get them to fit in a 2 byte unique binary column.我可以看到我的所有 mediumint(8) 值都是唯一的,并且它们的数值都小于 200,但由于某种原因,我无法让它们适合 2 字节的唯一二进制列。 My query keeps rejecting the insert because it says the values are larger than 2 bytes.我的查询一直拒绝插入,因为它说值大于 2 个字节。

sample query:示例查询:

select distinct id, part from partsTable
group by id
limit 100;

This comes back with a table that looks kind of like this这回来了一张看起来像这样的桌子

 id| part
_________
1  |  A
2  |  B
3  |  C
21 |  AB
22 |  AC
...

I have tried doing things like ( id & 0xFFFF) to force my id column as 2 bytes, and CAST(id as UNSIGNED) or CAST(id as BINARY(2)), but these always truncate the data so that i get duplicate values.我试过做 (id & 0xFFFF) 之类的事情来强制我的 id 列为 2 个字节,以及 CAST(id as UNSIGNED) 或 CAST(id as BINARY(2)),但这些总是截断数据,以便我得到重复值. Not really sure what I'm missing or how my unique id values that I can see are all unique get translated to non unique values when I try to cast them into binary.不确定我缺少什么,或者当我尝试将它们转换为二进制时,我可以看到的唯一 id 值如何全部被转换为非唯一值。

What's even more confusing is as a test I just inserted two records into my destination table:更令人困惑的是,作为测试,我刚刚将两条记录插入到目标表中:

INSERT into table1 (id, part) VALUES (0xFFFF, 'A');
INSERT into table1 (id, part) VALUES (0xFFFE, 'B');

And then I just wrote a query to see what that data looked like:然后我只是写了一个查询来查看数据的样子:

SELECT bin(id) as id, hex(id) as hex, part FROM table1;

Which returns a result like:返回的结果如下:

id  | hex   | part
__________________
0   | 0xFFFF| A
0   | 0xFFFE| B

So I have no idea what's going on所以我不知道发生了什么

2 bytes would be a numeric range of 65535. The problem here is the binary data type in MySQL still contains a string, not a number, but with collation of binary instead of something like utf8 as the char data type would. 2 个字节将是 65535 的数字范围。这里的问题是 MySQL 中的二进制数据类型仍然包含一个字符串,而不是一个数字,但使用二进制排序而不是像 char 数据类型那样的 utf8 之类的东西。 The difference is that the binary data type restricts the value based on the number of bytes rather than the number of characters in the string that it is storing.不同之处在于二进制数据类型根据字节数而不是它存储的字符串中的字符数来限制值。 In other words, a 3 digit value such as 150 as a string would be 3 characters, totaling 3 bytes.换句话说,一个 3 位数的值(例如 150)作为字符串将是 3 个字符,总共 3 个字节。 So if this value was converted to BINARY(2), it would lose anything past its 2nd byte, making it 15.因此,如果将此值转换为 BINARY(2),它将丢失超过其第二个字节的任何内容,使其成为 15。

Because it stores a string, the binary data type is not restricted to numeric characters (0,1) as you would expect either.因为它存储一个字符串,所以二进制数据类型并不像您期望的那样限制为数字字符 (0,1)。 So you could make the values fit in a BINARY(2) by using HEX() .因此,您可以使用HEX()使值适合BINARY(2) HEX() HEX(150) would be '96', and would therefore fit in a BINARY(2) . HEX(150)将是 '96',因此适合BINARY(2) You would then have to use UNHEX() to retreive any of these values.然后您必须使用UNHEX()来检索这些值中的任何一个。 If this table already has other values stored in it I doubt this would work for you though.如果这个表已经存储了其他值,我怀疑这对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM