douju2053 2017-01-06 12:36
浏览 445
已采纳

如何使用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

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

the recodrs will come like this:

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

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

$groupby = "GROUP BY student_id";
$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 12:51
    关注

    There are several ways to do this.

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

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

      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
      

      Your PHP would then look like

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

      select distinct student_pid
      ( select student_pid from ...
      union all
      select student_pid from ...
      union all
      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.

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看