2014-04-21 28 views
0

在下一個代碼中,我將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; 
} 
+0

您正在使用哪些DBMS? Postgres的?火鳥?甲骨文? –

+0

對不起,我忘記了,是MySQL – DavilaR

回答

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 
+0

對不起,我不是很明確,新表中的「匹配」已經存在於表格「候選人」中,我想用count()值填充它 – DavilaR

+0

@DavilaR檢查這個上面的查詢,讓我知道這是工作或不。更新查詢應該是你的匹配列表 –

+0

@Raja:一個簡短的解釋會有所幫助,你的答案看起來像「只是編輯別名在選擇」.--)簡單的魔法是UPDATE語句,更大的魔力是「into #TEMP」(這對我來說是新的,thx)。 – flaschenpost