关于 mysql 下同时使用 UUID()和 REPLACE()函数会发生重复的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
shalk
V2EX    数据库

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

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

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

    UUID()会生成 36 个字符,有时候需要 32 个字符就需要把- 去掉,通常会写成REPLAC(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     Solana     4117 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 05:31 PVG 13:31 LAX 22:31 JFK 01:31
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86