V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
shalk
V2EX  ›  数据库

关于 mysql 下同时使用 UUID()和 REPLACE()函数会发生重复的问题

  •  
  •   shalk · 2018-01-19 10:31:50 +08:00 · 741 次点击
    这是一个创建于 2283 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大家好,我遇到了一个奇怪的 SQL 问题,关于 mysql 下同时使用 UUID()和 REPLACE()函数会发生重复的问题。

    UUID()会生成 36 个字符,有时候需要 32 个字符就需要把- 去掉,通常会写成REPLACE(UUID(), "-", ""),这次我遇到了 100%出现重复的现象。

    下面是我测试的 SQL,可以清楚描述这个问题。

    select version() as 'mysql version';
    
    show variables like "%char%";
    
    DROP TABLE IF EXISTS mtest1;
    
    CREATE TABLE mtest1 ( f varchar(10) );
    
    INSERT INTO mtest1 (f) VALUES ("lily");
    
    INSERT INTO mtest1 (f) VALUES ("lucy");
    
    SELECT UUID() as uuid, f from mtest1;
    
    SELECT REPLACE(UUID(), "-", "") as uuid, f from mtest1;
    
    

    结果如下:

    	mysql version
    1	5.7.12-log
    
      	Variable_name	Value
    1	character_set_client	utf8mb4
    2	character_set_connection	utf8mb4
    3	character_set_database	utf8
    4	character_set_filesystem	binary
    5	character_set_results	utf8mb4
    6	character_set_server	utf8
    7	character_set_system	utf8
    8	character_sets_dir	C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\
    
      	uuid	f
    1	36ea391f-fcbe-11e7-b0df-001c42c9112f	lily
    2	36ea398f-fcbe-11e7-b0df-001c42c9112f	lucy
    
      	uuid	f
    1	36ea3e0ffcbe11e7b0df001c42c9112f	lily
    2	36ea3e0ffcbe11e7b0df001c42c9112f	lucy
    
    这里 uuid 发生重复。
    

    查到了一些网友的博客说改字符集就可以,但是原因不清楚。

    http://blog.csdn.net/hgg923/article/details/76643288

    http://blog.csdn.net/lwjdear/article/details/73187645

    http://www.cnblogs.com/tibit/p/6183864.html

    http://blog.csdn.net/LWJdear/article/details/73187656

    使用set names "utf8"改字符集:

    select version() as 'mysql version';
    
    set names "utf8";
    
    show variables like "%char%";
    
    
    DROP TABLE IF EXISTS mtest1;
    
    CREATE TABLE mtest1 ( f varchar(10) );
    
    INSERT INTO mtest1 (f) VALUES ("lily");
    
    INSERT INTO mtest1 (f) VALUES ("lucy");
    
    -- SELECT UUID(), f from mtest1;
    SELECT UUID() as uuid, f from mtest1;
    
    SELECT REPLACE(UUID(), "-", "") as uuid, f from mtest1;
    
    

    结果:

     	mysql version
    1	5.7.12-log
    
      	Variable_name	Value
    1	character_set_client	utf8
    2	character_set_connection	utf8
    3	character_set_database	utf8
    4	character_set_filesystem	binary
    5	character_set_results	utf8
    6	character_set_server	utf8
    7	character_set_system	utf8
    8	character_sets_dir	C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\
    
      	uuid	f
    1	ac12a0a6-fcbf-11e7-b0df-001c42c9112f	lily
    2	ac12a0e6-fcbf-11e7-b0df-001c42c9112f	lucy
    
      	uuid	f
    1	ac12a8e7fcbf11e7b0df001c42c9112f	lily
    2	ac12a921fcbf11e7b0df001c42c9112f	lucy
    
    这里产生 uuid 不重复。
    

    如果不改字符集,把 uuid()的结果 insert 一个临时表里,select 这个临时表时用 replace 也不会出问题。

    但是一旦在 utfmb4 字符集下,replace 和 uuid 一起用,就会重复。

    哪位高手知道这是为什么吗?

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2929 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 02:53 · PVG 10:53 · LAX 19:53 · JFK 22:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.