Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and 返回
一个很简单的SQL查询语句报错
Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLICIT) for operation '='
查询的方法如下:_db.Queryable<SysCodeGenConfig>() .Where(u => u.CodeGenId == input.CodeGenId && u.WhetherCommon != YesNoEnum.Y.ToString()) .Select<CodeGenConfig>() .Mapper(u => { u.NetType = (u.EffectType == "EnumSelector" || u.EffectType == "ConstSelector" ? u.DictTypeCode : u.NetType); }) .OrderBy(u => u.OrderNo)
生成的SQL如下:SELECT
IdAS
Id ,
CodeGenIdAS
CodeGenId ,
ColumnNameAS
ColumnName ,
PropertyNameAS
PropertyName ,
ColumnLengthAS
ColumnLength ,
ColumnCommentAS
ColumnComment ,
NetTypeAS
NetType ,
EffectTypeAS
EffectType ,
FkEntityNameAS
FkEntityName ,
FkTableNameAS
FkTableName ,
FkColumnNameAS
FkColumnName ,
FkColumnNetTypeAS
FkColumnNetType ,
DictTypeCodeAS
DictTypeCode ,
WhetherRetractAS
WhetherRetract ,
WhetherRequiredAS
WhetherRequired ,
QueryWhetherAS
QueryWhether ,
QueryTypeAS
QueryType ,
WhetherTableAS
WhetherTable ,
WhetherAddUpdateAS
WhetherAddUpdate ,
ColumnKeyAS
ColumnKey ,
DataTypeAS
DataType ,
WhetherCommonAS
WhetherCommon ,
DisplayColumnAS
DisplayColumn ,
ValueColumnAS
ValueColumn ,
PidColumnAS
PidColumn ,
OrderNoAS
OrderNoFROM
SysCodeGenConfig WHERE ((
CodeGenId= @CodeGenId0 ) AND (
WhetherCommon<> CAST(@MethodConst1 AS CHAR)))ORDER BY
OrderNoASC
执行时报错:
"Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLICIT) for operation '<>'"
如果将u.WhetherCommon != YesNoEnum.Y.ToString() 修改为 u.WhetherCommon != "Y" ,则可以避免这个问题
var query = _db.Queryable<SysCodeGenConfig>() .Where(u => u.CodeGenId == input.CodeGenId && u.WhetherCommon != "Y") .Select<CodeGenConfig>() .Mapper(u => { u.NetType = (u.EffectType == "EnumSelector" || u.EffectType == "ConstSelector" ? u.DictTypeCode : u.NetType); }) .OrderBy(u => u.OrderNo);
修改后生成的SQL语句如下:SELECT
IdAS
Id ,
CodeGenIdAS
CodeGenId ,
ColumnNameAS
ColumnName ,
PropertyNameAS
PropertyName ,
ColumnLengthAS
ColumnLength ,
ColumnCommentAS
ColumnComment ,
NetTypeAS
NetType ,
EffectTypeAS
EffectType ,
FkEntityNameAS
FkEntityName ,
FkTableNameAS
FkTableName ,
FkColumnNameAS
FkColumnName ,
FkColumnNetTypeAS
FkColumnNetType ,
DictTypeCodeAS
DictTypeCode ,
WhetherRetractAS
WhetherRetract ,
WhetherRequiredAS
WhetherRequired ,
QueryWhetherAS
QueryWhether ,
QueryTypeAS
QueryType ,
WhetherTableAS
WhetherTable ,
WhetherAddUpdateAS
WhetherAddUpdate ,
ColumnKeyAS
ColumnKey ,
DataTypeAS
DataType ,
WhetherCommonAS
WhetherCommon ,
DisplayColumnAS
DisplayColumn ,
ValueColumnAS
ValueColumn ,
PidColumnAS
PidColumn ,
OrderNoAS
OrderNoFROM
SysCodeGenConfig WHERE ((
CodeGenId= @CodeGenId0 ) AND (
WhetherCommon<> @WhetherCommon1 ))ORDER BY
OrderNoASC
我运行环境是.netcore 8.0 正式版,sqlsugar 5.1.4.118-preview02,mysql 8.0
请问应该如何修复?
一句话就是,linq里的
Where(u => u.WhetherCommon != YesNoEnum.Y.ToString())
会转成 以下语句导致异常
WHERE (WhetherCommon<> CAST(@MethodConst1 AS CHAR))
热忱回答(3)
-
fate sta VIP02023/11/19
tostring 提取到外面操作
0 回复 -
fate sta VIP02023/11/190 回复
-
fate sta VIP02023/11/19
这个是编码类型和cast as char 转换不兼容
tostring 提取到外面操作
var myString=yyy.ToString();
Where(x=>x.yyy=myString)
0 回复