在下一個代碼中,我將count()的別名顯示爲「coin」,但我想將它放入結果中名爲「matches」的列中,希望有人能夠幫助我。(SQL)如何將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
SOLUTION: 我放棄使用SQL來提取列「匹配」的值,所以我用休眠來做到這一點:
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;
}
您正在使用哪些DBMS? Postgres的?火鳥?甲骨文? –
對不起,我忘記了,是MySQL – DavilaR