douju2053 2017-01-06 04:36
浏览 446
已采纳

如何使用for循环和union all分组

Hi this is my query from this query i am getting 3 records i want only one record and i am trying to group by studentid

  1. foreach ($gdFinaldata['gdskill'] as $skillId => $skillScore){
  2. $filterQuery .= $union_all. "SELECT stu.student_pid
  3. FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email
  4. LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid
  5. WHERE job_id = ".$jobID." AND skill_id = ".$skillId." AND gd_score >= ".$skillScore." ";
  6. $union_all=" UNION ALL ";
  7. } //for ends here

the recodrs will come like this:

  1. 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
  2. UNION ALL
  3. 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
  4. UNION ALL
  5. 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

After this i want to add group by how can i do that i tried like this:

  1. $groupby = "GROUP BY student_id";
  2. $filterStudents = $conn->query($filterQuery.$groupby);

My issue is still returning 3 records i want to group by student

For Reference: enter image description here

  • 写回答

1条回答 默认 最新

  • dongxue7306 2017-01-06 04:51
    关注

    There are several ways to do this.

    • Use union instead of union all, this will retain only unique entries

      1. select ...
      2. union
      3. select ...
      4. union
      5. select ...
    • If you want to keep union all (why do you?), use parenthesis around your selects and append group by after it

      1. select student_pid
      2. ( select student_pid, student_id from ...
      3. union all
      4. select student_pid, student_id from ...
      5. union all
      6. select student_pid, student_id from ... ) as t
      7. group by student_id

      Your PHP would then look like

      1. foreach (...) {
      2. $filterQuery .= $union_all. "SELECT stu.student_pid, jsc.student_id ..."
      3. // ...
      4. }
      5. $outerQuery = "select student_pid ("
      6. $groupby = ") as t GROUP BY student_id";
      7. $filterStudents = $conn->query($outerQuery . $filterQuery . $groupby);
    • Instead of group by, you can also use distinct, e.g.

      1. select distinct student_pid
      2. ( select student_pid from ...
      3. union all
      4. select student_pid from ...
      5. union all
      6. select student_pid from ... ) as t
    • Collect the result in an array and then use array_unique Although this is the least efficient, since it fetches all entries instead of the relevant parts.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部