【问题标题】:Issue with concat function hibernate in hql queryhql查询中的concat函数休眠问题
【发布时间】:2022-02-04 09:31:34
【问题描述】:

我有一个带有动态值的 hql 查询,看起来像这样


    SELECT * from sample_view where (concat(course_id,'-',stu
    _id)) in ('11-001'));

以上查询返回 4 行,但具有以下值的相同查询不返回任何内容


    SELECT * from sample_view where (concat(course_id,'-',stu
    _id)) in ('011-001'));

当我执行以下这些查询时,它们都返回 4 行。

SELECT * from sample_view where course_id in ('011') and stu_id in ('001');

SELECT * from sample_view where course_id in ('11') and stu_id in ('001');

当输入的数字以 0 为前缀时,如何使 concat 函数返回相同的值。在这方面的任何帮助将不胜感激。提前致谢

【问题讨论】:

    标签: spring database hibernate concatenation hql


    【解决方案1】:

    首先,不要在问题的开头使用连接技巧。使用完整查询并检查两列:

    SELECT *
    FROM sample_view
    WHERE course_id = ? AND stu_id = ?;
    

    如果您想忽略任一 ID 中的前导零,只需在将值绑定到语句之前去掉这些零,例如

    String courseId = "011";
    courseId = courseId.replaceFirst("^0+", "");
    System.out.println(courseId); // 11
    // bind course ID here to your statement
    

    【讨论】:

      猜你喜欢
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      相关资源
      最近更新 更多