如何在MySQL / MariaDB中从二进制列格式化uuid字符串

时间:2016-05-11 16:49:26

标签: mysql mariadb uuid

在MySQL / MariaDB中,存储uuid的最有效方法是在BINARY(16)列中。但是,有时您希望将其作为格式化的uuid字符串获取。

鉴于以下表格结构,我如何以默认格式化方式获取所有uuids?

CREATE TABLE foo (uuid BINARY(16));

7 个答案:

答案 0 :(得分:39)

以下将创建我之后的结果:

SELECT
  LOWER(CONCAT(
    SUBSTR(HEX(uuid), 1, 8), '-',
    SUBSTR(HEX(uuid), 9, 4), '-',
    SUBSTR(HEX(uuid), 13, 4), '-',
    SUBSTR(HEX(uuid), 17, 4), '-',
    SUBSTR(HEX(uuid), 21)
  ))
FROM foo;

答案 1 :(得分:9)

MySQL 8添加了two new UUID functions

所以:

SELECT BIN_TO_UUID(uuid) FROM foo

答案 2 :(得分:2)

在早期(早于8个)版本中,您可以在MySQL中创建function,如下所示:

CREATE
  FUNCTION uuid_of(uuid BINARY(16))
  RETURNS VARCHAR(36)
  RETURN LOWER(CONCAT(
  SUBSTR(HEX(uuid), 1, 8), '-',
  SUBSTR(HEX(uuid), 9, 4), '-',
  SUBSTR(HEX(uuid), 13, 4), '-',
  SUBSTR(HEX(uuid), 17, 4), '-',
  SUBSTR(HEX(uuid), 21)
));

然后在查询中使用它:

SELECT
  uuid_of(id)
  name,
  age
FROM users

它产生:

  

(c6f5703b-fec2-43fd-8f45-45f06583d450,有些名字,20)

答案 3 :(得分:2)

如果您正在寻找相反的方向,即,如何从字符串转换为二进制,或者执行联接等操作,请参见:Convert UUID to/from binary in Node

在Mysql 5.7上运行的这段SQL对我来说帮助锁定了这个概念:

SELECT
  LOWER(CONCAT(
    SUBSTR(HEX(UNHEX(REPLACE('43d597d7-2323-325a-90fc-21fa5947b9f3', '-', ''))), 1, 8), '-',
    SUBSTR(HEX(UNHEX(REPLACE('43d597d7-2323-325a-90fc-21fa5947b9f3', '-', ''))), 9, 4), '-',
    SUBSTR(HEX(UNHEX(REPLACE('43d597d7-2323-325a-90fc-21fa5947b9f3', '-', ''))), 13, 4), '-',
    SUBSTR(HEX(UNHEX(REPLACE('43d597d7-2323-325a-90fc-21fa5947b9f3', '-', ''))), 17, 4), '-',
    SUBSTR(HEX(UNHEX(REPLACE('43d597d7-2323-325a-90fc-21fa5947b9f3', '-', ''))), 21)
  ))

输出为43d597d7-2323-325a-90fc-21fa5947b9f3

字符串->二进制

因此UNHEX(REPLACE('43d597d7-2323-325a-90fc-21fa5947b9f3', '-', ''))可以在INSERT / UPDATE / JOIN / SELECT期间将UUID转换为二进制,并且

binary->字符串

LOWER(CONCAT(
  SUBSTR(HEX(uuid), 1, 8), '-',
  SUBSTR(HEX(uuid), 9, 4), '-',
  SUBSTR(HEX(uuid), 13, 4), '-',
  SUBSTR(HEX(uuid), 17, 4), '-',
  SUBSTR(HEX(uuid), 21)
))

答案 4 :(得分:0)

这是使用concat_ws

的替代方法

将原始uuid存储在变量@x中

    Browser.Goto("https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-54365007M8453270M#/checkout/login");
        Browser.SwitchToIframe("injectedUl");

     var email = driver.FindElement(By.ID("email"));
                email.SendKeys("test@no.com");

                var Password = driver.FindElement(By.ID("password"));
                Password.SendKeys("xxxxxxxxxx");

                var Login = driver.FindElement(By.Id("btnLogin"));
                Login.Click();

使用CONCAT_WS和SUBSTR解析人类可读的UUID

SELECT @x := hex(uuid)
FROM foo;

答案 5 :(得分:0)

正确的结果是由以下脚本生成的,其他脚本已生成了UUID,但不是正确的。

public NotificationCompat.Builder getChannelNotification()

运行其他脚本的结果生成错误的UUID,如下所示:

  • 预期的UUID-CONCAT( substr(hex(Id), 7, 2), substr(hex(Id), 5, 2), substr(hex(Id), 3, 2), substr(hex(Id), 1, 2), '-' , substr(hex(Id), 11, 2) , substr(hex(Id), 9, 2) , '-' , substr(hex(Id), 15, 2) , substr(hex(Id), 13, 2) , '-' , substr(hex(Id), 17, 4) , '-' , substr(hex(Id), 21, 12) )
  • 生成的内容-2e9660c2-1e51-4b9e-9a86-6db1a2770422

如您所见,它们是不同的。

答案 6 :(得分:0)

根据此 Jira 票证 https://jira.mariadb.org/browse/MDEV-15854 UUID_TO_BIN 和 BIN_TO_UUID 没有进入 Mariadb 服务器版本 10.5。如果您正在使用此版本和 Mariadb Server 下的版本,则必须使用上述自定义实现。