dongxia527680 2014-04-24 08:13
浏览 226
已采纳

在MySQL中将多行显示为一行

I need some advice on

I created a web app where a user can assign a form to multiple recipients simultaneously so that one assignment inserts multiple rows (one row per recipient) into the form_assignments table like so:

foreach ($recipients as $row) {
  $query = "INSERT INTO form_assignments 
       (sender_id, recipient_id, form_id, due_date, priority_id, comment, completed) 
       VALUES 
       ('{$sender_id}', " . $row['id'] . ", '{$assigned_form_id}', '2014-04-30 00:00:00', '{$priority_id}', '{$comment}', '{$completed}')";

In the example above, $recipients is an array with user ids referenced by $row['id'].

So, one assignment can generate 100 of these rows (for 100 recipients), for example, and each recipient can edit his individual row as "completed". This is good so far.

The problem is, if I want to generate a list of assignments for the user that assigned them I want him to see only one row for a multiple-user assignment (not one row for each recipient he sent the assignment to).

I'd love to be able to show the user a table like:

You assigned: form 1 To: 50 users Due on: April 25, 2014 Users Completed: 10 View List of Users

And each assignee would see a table like:

User 1 assigned you the following form: form 1 Due on: April 25, 2014, Click to Mark as Completed

I'm not sure how to go about this. If anyone could point me in the right direction I would greatly appreciate it.

  • 写回答

2条回答 默认 最新

  • dongtu7205 2014-04-24 08:30
    关注

    If i do understand you correctly, you want to present to the person that assigns, a list of assignments, grouped by assignment and date. For each Assignment/Date Result, you alos want to present how many Users already completed the Assignment right.

    So you want to Select from assignments and Group your Result by sender_id, form_id and due_date first:

    SELECT sender_id, form_id, due_date
    FROM assignments as groupedAssignments
    GROUP BY 
      groupedAssignments.sender_id, 
      groupedAssignments.form_id, 
      groupedAssignments.due_date
    

    Now you also want to JOIN the assignments to get th count for how many students already finished this assignment. That's why i added the alias 'groupedAssignments'. So add a JOIN After your FROM

    JOIN assignments as finishedAssignments ON (
      groupedAssignments.sender_id = finishedAssignments.sender_id
      AND groupedAssignments.form_id = finishedAssignments.form_id
      AND groupedAssignments.due_date = finishedAssignments.due_date
      AND finishedAssignments.completed = TRUE )
    

    Now you can add a Count on the finished assignments giving you the full Query:

    SELECT 
      groupedAssignments.sender_id, 
      groupedAssignments.form_id, 
      groupedAssignments.due_date, 
      COUNT(finishedAssignments.completed) as finishedStudents
    FROM assignments as groupedAssignments
    JOIN assignments as finishedAssignments ON (
      groupedAssignments.sender_id = finishedAssignments.sender_id 
      AND groupedAssignments.form_id = finishedAssignments.form_id
      AND groupedAssignments.due_date = finishedAssignments.due_date
      AND finishedAssignments.completed = TRUE )
    GROUP BY 
      finishedAssignments.sender_id, 
      finishedAssignments.form_id, 
      finishedAssignments.due_date
    

    Obviously i didn't test this and it might not work for you, but it should lead you in the right direction. Read up on GROUP BY and JOIN.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决