将多个选择语句结果存储到一个变量中

时间:2019-01-24 17:08:45

标签: mysql mysqli

我有这个查询,在当前情况下运行良好。但是,由于php代码清晰可见,因此在mysql查询执行后便完成了点计算。我想在MySQL查询中计算点数,以允许用户过滤得分的结果基础点。

简而言之,我想直接从mysql查询的每一行中实现 $ finalpoints 变量输出。

我以这样的方式思考,但无法弄清楚。请提出任何更好的方法来实现此目的。

if(count(DISTINCT j.e_qua_typ) as t_edu >0, @x+=20,0)

完整查询

SET @x = 0;
select 
    a.id, a.fname, a.lname, b.u_image_path, a.activation, b.m_no, b.u_salary, b.u_not_period, b.u_job_typ, count(c.w_u_id) as   
    Total_org_count, SUM(COALESCE(DATEDIFF(COALESCE(c.w_end_dt,CURDATE()), c.w_start_dt),0)) total_days,
    (select CONCAT(d.w_designation,'-',d.w_comp_name) AS ConcatenatedString from u_work_exp d where d.w_is_current='Y' and 
     d.w_u_id=a.id) as designation,
    (select count(e.ref_by_uid) from job_referrals e where e.ref_by_uid=a.id and (CURRENT_DATE-e.ref_datetime)<=90) as t_referral,
    (select count(f.end_by_uid) from s_endorsements f where f.end_by_uid=a.id and (CURRENT_DATE-f.end_datetime)<=90) as t_endorsements,
    (select count(h.seek_id) from s_follow_t h where h.seek_id=a.id and (CURRENT_DATE-h.follow_date)<=90) as t_follow,
    count(DISTINCT j.e_qua_typ) as t_edu,
    count(DISTINCT k.s_u_id) as t_skills,
    count(DISTINCT l.l_u_id) as t_lang,
    count(DISTINCT c.w_u_id) as t_work,
    (select count(m.uid) from login_history m where m.uid=a.id and (CURRENT_DATE-m.Login_date_time)<=15) as t_activeness,
    count(DISTINCT n.seek_id) as t_comp_review,
    count(DISTINCT o.blog_owner_id) as t_blogs,
    (select count(p.m_u_id) from group_members p where p.m_u_id=a.id and (CURRENT_DATE-p.m_join_date)<=90) as t_groups,
    count(DISTINCT q.course_id) as t_learning_bought
from usertables a
    LEFT JOIN u_dtls b on b.u_id=a.id
    LEFT JOIN u_work_exp c on a.id=c.w_u_id
    LEFT JOIN u_edu_dtls j on j.e_u_id=a.id
    LEFT JOIN u_skills_dtls k on k.s_u_id=a.id
    LEFT JOIN u_lang_dtls l on l.l_u_id=a.id
    LEFT JOIN s_company_review n on n.seek_id=a.id
    LEFT JOIN blogs o on o.blog_owner_id=a.id
    LEFT JOIN learnings_buy_tb q on q.lrn_user_id=a.id
group by a.id
order by a.id desc

PHP点计算

$points = "";
//calculate scoring points
if($row['t_referral']>0){ $points+=25; }
if($row['t_endorsements']>0){ $points+=5; }
if($row['t_follow']>0){ $points+=10; }
if(!empty($row['u_salary']) && $row['t_edu']>0 && $row['t_skills']>0 && $row['t_lang']>0 && $row['t_work']>0){
    $points+=20; 
}
if($row['t_activeness']>0){ $points+=5; }
if($row['t_comp_review']>0){ $points+=10; }
if($row['t_blogs']>0){ $points+=10; }
if($row['t_groups']>0){ $points+=5; }
if($row['t_learning_bought']>0){ $points+=10; }

$finalpoints = $points / 20;

1 个答案:

答案 0 :(得分:0)

您可以将查询包装为派生表子查询,然后在外部查询中使用表达式来计算最终点:

"p"

我不确定SELECT ( IF(t_referral>0, 25, 0) + IF(t_endorsements>0, 5, 0) + IF(t_follow>0, 10, 0) + IF(u_salary>0 AND t_edu>0 AND t_skills>0 AND t_lang>0 AND t_work>0, 20, 0) + IF(t_activeness>0, 5, 0) + IF(t_comp_review>0, 10, 0) + IF(t_blogs>0, 10, 0) + IF(t_groups>0, 5, 0) + IF(t_learning_bought>0, 10, 0) )/20 AS finalpoints FROM ( ...your query... ) AS t 是什么,还是为什么它不是空的而不是0。因此,请随时为您的数据修改此解决方案,但是原理是我要说明的。 / p>