Php 如何使用forloop和union all按列分组
您好,这是我的查询,我从这个查询中得到了3条记录,我只想要一条记录,我正试图按Php 如何使用forloop和union all按列分组,php,mysql,foreach,group-by,union,Php,Mysql,Foreach,Group By,Union,您好,这是我的查询,我从这个查询中得到了3条记录,我只想要一条记录,我正试图按studentid foreach ($gdFinaldata['gdskill'] as $skillId => $skillScore){ $filterQuery .= $union_all. "SELECT stu.student_pid FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.e
studentid
foreach ($gdFinaldata['gdskill'] as $skillId => $skillScore){
$filterQuery .= $union_all. "SELECT stu.student_pid
FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email
LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid
WHERE job_id = ".$jobID." AND skill_id = ".$skillId." AND gd_score >= ".$skillScore." ";
$union_all=" UNION ALL ";
} //for ends here
记录将如下所示:
SELECT stu.student_pid FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid WHERE job_id = 88 AND skill_id = 3 AND gd_score >= 1
UNION ALL
SELECT stu.student_pid FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid WHERE job_id = 88 AND skill_id = 63 AND gd_score >= 2
UNION ALL
SELECT stu.student_pid FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid WHERE job_id = 88 AND skill_id = 128 AND gd_score >= 3
$groupby = "GROUP BY student_id";
$filterStudents = $conn->query($filterQuery.$groupby);
在此之后,我想通过以下方法添加组:
SELECT stu.student_pid FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid WHERE job_id = 88 AND skill_id = 3 AND gd_score >= 1
UNION ALL
SELECT stu.student_pid FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid WHERE job_id = 88 AND skill_id = 63 AND gd_score >= 2
UNION ALL
SELECT stu.student_pid FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid WHERE job_id = 88 AND skill_id = 128 AND gd_score >= 3
$groupby = "GROUP BY student_id";
$filterStudents = $conn->query($filterQuery.$groupby);
我的问题仍然是返回3个记录,我想按学生分组
供参考:
有几种方法可以做到这一点
- 使用
而不是union
,这将只保留唯一的条目union all
select ... union select ... union select ...
- 如果要保留
(为什么?),请在所选内容周围使用括号,并在其后面附加union all
groupby
然后,您的PHP看起来像select student_pid ( select student_pid, student_id from ... union all select student_pid, student_id from ... union all select student_pid, student_id from ... ) as t group by student_id
foreach(…){ $filterQuery.=$union\u all.“选择stu.student\u pid,jsc.student\u id…” // ... } $outerQuery=“选择学生\u pid(” $groupby=“)作为t组按学生id”; $filterStudents=$conn->query($outerQuery.$filterQuery.$groupby);
- 您也可以使用
,而不是distinct
分组依据,例如
select distinct student_pid ( select student_pid from ... union all select student_pid from ... union all select student_pid from ... ) as t
- 在数组中收集结果,然后使用 尽管这是效率最低的,因为它获取所有条目而不是相关部分