简体   繁体   English

我如何将此 mysql 语句转换为 hql

[英]How i can convert this mysql statement to hql

How i can convert this mysql statement to hql我如何将此 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

my try was我的尝试是

"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"

but i got this error:但我收到了这个错误:

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

turned out that hql does not support subqueries原来hql不支持子查询

ps: i need the invisit , notinVisit props as constructor params to return an object containing the result ps:我需要invisitnotinVisit道具作为构造参数来返回包含结果的 object

Try to use this尝试使用这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM