SelectModel 生成查询字段时无法兼容json查询 返回

SqlSugar 沟通中
14 241
new SelectModel { FieldName = $"a.OtherMeta->>'$.{column.KeyWord}'", AsName = column.KeyWord})

期望生成的sql是

SELECT  `a`.`OtherMeta`->>'$.zuhao' as zuhao

实际生成的是

SELECT  `a`.`OtherMeta->>'$`.`zuhao'` AS `zuhao`

应是添加反引号时识别到了$后面的.导致json查询作为字段时被反引号截断了

热忱回答14

  • fate sta fate sta VIP0
    1个月前

    这个要用函数

    0 回复
  • fate sta fate sta VIP0
    1个月前

    看一下selectmodel函数用法


    0 回复
  • fate sta fate sta VIP0
    1个月前

    对应JsonField

    0 回复
  • fate sta fate sta VIP0
    1个月前

    或者使用SQL


    Select<object>("id,name")

    0 回复
  • Dnine7 Dnine7 VIP0
    1个月前

    使用查询函数

    new SelectModel { FieldName = ObjectFuncModel.Create("JsonField","a.OtherMeta",column.KeyWord), AsName = column.KeyWord})

    生成的sql依旧有误

    SELECT   `a`.`OtherMeta`->'$.`zuhao`' AS `zuhao` 
    报错 MySqlConnector.MySqlException (0x80004005): Invalid JSON path expression. The error is around character position 8.  (sql前面有无关内容被我截取了)

    0 回复
  • fate sta fate sta VIP0
    1个月前

    这个是什么东西

    column.KeyWord

    是不是按文档中格式填写。

     

    0 回复
  • Dnine7 Dnine7 VIP0
    1个月前

    这个是传进来的参数 本质是个字符串 "zuhao"

    0 回复
  • Dnine7 Dnine7 VIP0
    1个月前

    image.png
    按照这个写的

    0 回复
  • fate sta fate sta VIP0
    1个月前

    提供你具体写的代码

    0 回复
  • Dnine7 Dnine7 VIP0
    1个月前
    var ls = new List<SelectModel>();
    var column = new
    {
       KeyWord = "fuhe"
    };
    ls.Add(new SelectModel { FieldName = "Id", AsName = "Id"});
    ls.Add(new SelectModel { FieldName = ObjectFuncModel.Create("JsonField","OtherMeta",column.KeyWord), AsName = column.KeyWord});

    var rt = await db.Queryable<ArchiveFile>().Select(ls).ToDataTablePageAsync(1,20);


    0 回复
  • Dnine7 Dnine7 VIP0
    1个月前

    生成的sql如下
    SELECT `Id` AS `Id` , `OtherMeta`->'$.`fuhe`' AS `fuhe` FROM `ArchiveFile`     LIMIT 0,20

    0 回复
  • 11111111 11111111 VIP0
    1个月前

    写法错误,变量有类型的。

    image.png

    0 回复
  • Dnine7 Dnine7 VIP0
    1个月前

    @11111111

    是的 
    写成这样就解决了 万分感谢

    new SelectModel { FieldName = ObjectFuncModel.Create("JsonField","a.OtherMeta","{string}:fuhe"), AsName = "fuhe"}


    0 回复
  • Dnine7 Dnine7 VIP0
    1个月前

    @fate sta还有一个问题, JsonField生成出的sql 是 JsonObject->'$.item'   返回的json数据是带引号的 有函数可以返回不带引号的数据吗? 即JsonObject->>'$.item'

    0 回复