簡體   English   中英

我如何將此 mysql 語句轉換為 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

我的嘗試是

"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道具作為構造參數來返回包含結果的 object

嘗試使用這個

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