Mysql (SQL)如何将count()放入已存在的列中?
在下一段代码中,我显示了count as coin的别名,但我想将其放入结果中名为matches的列中,希望有人能帮助我 候选表中已存在“新列匹配项”,我想用计数值填充它 SQL代码:Mysql (SQL)如何将count()放入已存在的列中?,mysql,sql,hibernate,Mysql,Sql,Hibernate,在下一段代码中,我显示了count as coin的别名,但我想将其放入结果中名为matches的列中,希望有人能帮助我 候选表中已存在“新列匹配项”,我想用计数值填充它 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 (
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;
}
刚把硬币换成了“选择和排序”中的匹配项
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
您使用的是哪种数据库管理系统?博士后?火鸟?Oracle?很抱歉我忘了,是MySQLI吗?很抱歉我不是很明确,新的列匹配项“已存在于候选表中,我想用count填充它values@DavilaR检查上面的查询,让我知道这是否有效。这里更新查询应该是您的匹配列table@Raja:简短的解释会有帮助,您的答案看起来像是在“选择”对话框中编辑了别名简单的魔法是UPDATE语句,更大的魔法是into TEMP,这对我来说是新的,thx。我尝试了,但回答是:错误代码:1064。您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,了解在第行的“select c.*from jobweb.c,jobweb.additional_knowledge ak where ak.c”附近使用的正确语法2@RajaRajendraprasath我不知道sintaxis Temp是否正常,因为它使Temp之后的所有句子都变成灰色