(SQL)如何将count()放入已存在的列中?

时间:2014-04-21 12:39:02

标签: mysql sql hibernate

在下一个代码中,我将count()的别名显示为“coin”,但我想将其放入结果中名为“matches”的列中,希望有人能帮助我。

表格“候选人”中已经存在新列“匹配”,我想用count()值填充

SQL代码:

SELECT table1.* , count(*) as coin from (
(SELECT c.* from jobweb.candidates c, jobweb.additional_knowledge ak where
(ak.candidate_id = c.candidate_id) and (ak.knowledge like '%ccna%' or 
ak.knowledge_description like '%ccna%' or 
ak.knowledge like '%java%' or ak.knowledge_description like '%java%'))
union all
(SELECT c.* from jobweb.candidates c , jobweb.work_experience we where 
( we.candidate_id = c.candidate_id ) and
( we.position_name like '%sdh%' or we.functions_desciption like '%sdh%' or 
we.position_name like '%sharepoint%' or we.functions_desciption like '%sharepoint%' or 
we.position_name like '%proyecto%' or we.functions_desciption like '%proyecto%' or 
we.position_name like '%ingeniero%' or we.functions_desciption like '%ingeniero%' ))
union all
(SELECT c.* from jobweb.candidates c, jobweb.formal_education fe where
(fe.candidate_id =  c.candidate_id and fe.education_description like '%ingeniero%'))
)  as table1 group by table1.candidate_id order by coin desc

解: 我放弃使用SQL来提取“匹配”列上的值,所以我使用了hibernate来做到这一点:

public List<Candidate> getCandidatesMatchesNativeSQL(String customQuery) {
    Query query = sessionFactory.getCurrentSession().createSQLQuery(customQuery)
            .addEntity(Candidate.class)
            .addScalar("matchCounter");
    @SuppressWarnings("unchecked")
    List<Object[]> objects = query.list();
    List<Candidate> candidates = new ArrayList<Candidate>();
    for (Object[] object : objects ) {
        Candidate candidate = (Candidate) object[0];
        BigInteger match = (BigInteger) object[1];
        candidate.setMatches( match.intValue() );
        candidates.add(candidate);
    }
    return candidates;
}

1 个答案:

答案 0 :(得分:1)

刚刚在选择和顺序中将硬币更改为匹配。

       CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS 
   (SELECT table1.candidate_id , count(*) as coin   from (
    (SELECT c.* from jobweb.candidates c, jobweb.additional_knowledge ak where
    (ak.candidate_id = c.candidate_id) and (ak.knowledge like '%ccna%' or 
    ak.knowledge_description like '%ccna%' or 
    ak.knowledge like '%java%' or ak.knowledge_description like '%java%'))
    union all
    (SELECT c.* from jobweb.candidates c , jobweb.work_experience we where 
    ( we.candidate_id = c.candidate_id ) and
    ( we.position_name like '%sdh%' or we.functions_desciption like '%sdh%' or 
    we.position_name like '%sharepoint%' or we.functions_desciption like '%sharepoint%' or 
    we.position_name like '%proyecto%' or we.functions_desciption like '%proyecto%' or 
    we.position_name like '%ingeniero%' or we.functions_desciption like '%ingeniero%' ))
    union all
    (SELECT c.* from jobweb.candidates c, jobweb.formal_education fe where
    (fe.candidate_id =  c.candidate_id and fe.education_description like '%ingeniero%'))
    )  as table1 group by table1.candidate_id order by coin desc)

    Update A set A.matches=B.coin from 
    candidates A inner join table2 B on A.candidate_id=B.candidate_id