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 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现