MySql数据库字段排序规则不一致产生的一个问题
最近项目向MySql迁移,迁移完毕后,在获取用户权限时产生了一个异常,跟踪进去获取执行的语句如下,
SELECT PermissionId FROM spysxtPermission WHERE (ResourceCategory = 'BaseUser' AND ResourceId = '003bf4bd072243fa90517ee2bc088cb7' AND Enabled = 1 AND DeletionStateCode = 0) UNION SELECT PermissionId FROM spysxtPermission , ( SELECT RoleId FROM spysxtUserRole WHERE (UserId = '003bf4bd072243fa90517ee2bc088cb7' AND Enabled = 1 AND DeletionStateCode = 0 ) UNION SELECT RoleId FROM BaseUserRole WHERE ( UserId = '003bf4bd072243fa90517ee2bc088cb7' AND Enabled = 1 AND DeletionStateCode = 0 ) ) B WHERE ResourceCategory = 'spysxtRole' AND spysxtPermission.ResourceId = B.RoleId AND spysxtPermission.Enabled = 1 AND spysxtPermission.DeletionStateCode = 0
在Navicate执行,提示如下:
这个语句在Oracle中执行都是正常的,一时不知道是什么原因,baidu了也没找到如何处理,我将语句逐个拆开执行,最终找到问题出在这里:
SELECT RoleId FROM spysxtUserRole WHERE (UserId = '003bf4bd072243fa90517ee2bc088cb7' AND Enabled = 1 AND DeletionStateCode = 0 )
UNION SELECT RoleId FROM BaseUserRole WHERE ( UserId = '003bf4bd072243fa90517ee2bc088cb7' AND Enabled = 1 AND DeletionStateCode = 0 )
看到这里,我突然想到有可能是字符集不一样造成的,打开表
原来问题出在排序规则上,一个是utf8_unicode_ci,一个是utf8_danish_ci。全部改为utf8_unicode_ci。问题解决!
原以为是代码bug,一直在调试代码,没想到问题出在这里。