Java 从多个表中选择计数(*)

Java 从多个表中选择计数(*),java,mysql,sql,hibernate,Java,Mysql,Sql,Hibernate,亲爱的朋友们,我需要从一个具有不同标准的表中获取计数(*)。我正在使用hibernate和spring。我正在使用下面的查询 "select " + "(select count(*) from enquiry where business_id="+busid+") AS allEnqCount,"+ "(select count(*) from enquiry where status='"+Constants.ENQ_FALLOWUP+"' and

亲爱的朋友们,我需要从一个具有不同标准的表中获取计数(*)。我正在使用hibernate和spring。我正在使用下面的查询

"select  " +
            "(select count(*)  from enquiry  where business_id="+busid+")  AS allEnqCount,"+
    "(select count(*)  from enquiry where status='"+Constants.ENQ_FALLOWUP+"' and us.business_id="+busid+")  AS followupCount," +
    "(select count(*)  from enquiry where status='"+Constants.ENQ_SITE_VISIT+"'and us.business_id="+busid+")  AS siteVisitCount ," +
    "(select count(*)  from enquiry where status='"+Constants.ENQ_CUST_VISIT+"'and us.business_id="+busid+")  AS customerVisitCount," +
    "(select count(*)  from enquiry where status='"+Constants.ENQ_OFFICE_VISIT+"'and us.business_id="+busid+")  AS officevisitCount,"+
    "(select count(*)  from enquiry where status='"+Constants.ENQ_PENDING+"'and us.business_id="+busid+")  AS pending";
但这需要相当长的时间。你能告诉我有没有什么办法,我可以用最短的时间和一个选择条款来做这件事

提前感谢。

使用
案例

SELECT  SUM(CASE WHEN business_id = 'busid' THEN 1 END) allEnqCount,
        SUM(CASE WHEN status = 'ENQ_FALLOWUP' AND us.business_id = 'busid' THEN 1 END) followupCount,
        SUM(CASE WHEN status = 'ENQ_SITE_VISIT' AND us.business_id = 'busid' THEN 1 END) siteVisitCount,
        SUM(CASE WHEN status = 'ENQ_CUST_VISIT' AND us.business_id = 'busid' THEN 1 END) customerVisitCount,
        SUM(CASE WHEN status = 'ENQ_OFFICE_VISIT' AND us.business_id = 'busid' THEN 1 END) officevisitCount,
        SUM(CASE WHEN status = 'ENQ_PENDING' AND us.business_id = 'busid' THEN 1 END) pending
FROM    enquiry

只需更改满足您需要的值。

使用
分组方式

比如:

select 
      status,
      count(*) 
from enquiry 
where business_id = @busid 
group by status

谢谢JW。但所用的时间与以前相同。请尝试在状态和业务id上添加
INDEX
,例如,
ALTER TABLE inquiry ADD INDEX(business\u id,status)
,然后再次执行查询。除添加索引外,还应写入count(1)以代替count(*)。这里1是主键列的索引。@Andreas:谢谢,伙计,我肯定会尽我所能澄清这个问题,但在那之前,我会用你的概念来解释这个表中有多少行?