【问题标题】:How i can convert this mysql statement to hql我如何将此 mysql 语句转换为 hql
【发布时间】:2020-08-31 17:12:14
【问题描述】:

我如何将这个 mysql 语句转换为 hql

    select invisit, notinVisit
    FROM(
    select COUNT(*) as invisit  from consultations cons join patients p 
    on cons.patient_id = p.id
    where cons.datefin > now()) as A
    JOIN 
    ( select COUNT(*) as notinVisit from consultations cons join patients p 
    on cons.patient_id = p.id
    where cons.datefin < now()) as B

我的尝试是

"select com.organ.project.service.DTO.PatientsByInConsultationDTO(invisit, notinVisit)\n" +
            "FROM(\n" +
            "select COUNT(*) as invisit  from Consultation cons join Patient p \n" +
            "on cons.patient.id = p.id\n" +
            "where cons.datefin > now()) as A\n" +
            "JOIN \n" +
            "( select COUNT(*) as notinVisit from Consultation cons join Patient p \n" +
            "on cons.patient.id = p.id\n" +
            "where cons.datefin < now()) as B\n"

但我收到了这个错误:

 ...org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 2, column 5...

原来hql不支持子查询

ps: 我需要 invisitnotinVisit 属性作为构造函数参数来返回包含结果的对象

【问题讨论】:

    标签: mysql hibernate jpa spring-data-jpa hql


    【解决方案1】:

    尝试使用这个

    select new com.organ.project.service.DTO.PatientsByInConsultationDTO(
           sum(case when cons.datefin > now() then 1 else 0 end) as invisit,
           sum(case when cons.datefin < now() then 1 else 0 end) as notinVisit) 
    from Consultation cons join Patient p on cons.patient.id = p.id
    group by cons.id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      相关资源
      最近更新 更多