2

我一直在尝试一切我能做到的技巧来完成我正在寻找的东西。我不是菜鸟,但我坚持这一点。我正在尝试从 3 个表中创建一个数据“网格”。我希望它看起来像这样:

  Assignments | Assgn 1 | Assgn 2 | Assgn 3 | Assgn 4 | Final Grade
----------------------------------------------------------------------
   Username 1 |  Grade  |  Grade  |  Grade  |  Grade  | Total  
----------------------------------------------------------------------
   Username 2 |  Grade  |  Grade  |  Grade  |  Grade  | Total  
----------------------------------------------------------------------
   Username 3 |  Grade  |  Grade  |  Grade  |  Grade  | Total  

成员的第一个表列:

Memberid, Username

分配表:

 id, title

成绩保存在 subassign 中:

id,assign,student,grade

assign 是 assignments 表中作业的 id 号 student 是 members 表中的 Memberid。

我已经研究了几天。

给出一些结果的是这个查询

 $result = mysql_query("SELECT assignments.title,subassign.grade, members.Username FROM assignments, subassign, members WHERE members.Memberid = subassign.student and assignments.id = subassign.assign ",$connect);

它正确链接了信息,但我无法让它像我想要的那样显示记录。它只是在 php.ini 中创建一个新行。有人可以帮忙吗。我正要把我的电脑扔出窗外。

4

3 回答 3

1

您可以像这样使用连接:

SELECT
    members.title AS 'Assignments',
    assignment1.grade AS 'Assign1',
    assignment2.grade AS 'Assign2',
    assignment3.grade AS 'Assign3',
    assignment3.grade AS 'Assign4'
FROM members
INNER JOIN subassign AS assignment1 ON assignment1.person = members.MemberId AND assign = 1
INNER JOIN subassign AS assignment2 ON assignment2.person = members.MemberId AND assign = 2
INNER JOIN subassign AS assignment3 ON assignment3.person = members.MemberId AND assign = 3
INNER JOIN subassign AS assignment4 ON assignment4.person = members.MemberId AND assign = 4

请注意,如果您想根据表中的返回动态列(我假设您这样做)assignments,最好的办法是动态生成类似于上面的查询。请参阅下面的一些非常粗略的示例代码,这些代码会生成您想要的动态查询(这完全未经测试):

$results = mysql_query("SELECT id, title FROM assignments");
$query = "SELECT
    members.title AS 'Assignments',";

$joins = "";

while($row = mysql_fetch_assoc($results))
{
    $name = 'assignment' . $row['id'];
    $query .= "\r\n" . $name . ".grade AS '" . $row['title'] . "',";
    $joins .= "\r\nLEFT JOIN subassign AS " . $name . " ON " . $name . ".person = members.MemberId AND " . $name . ".assign = " . $row['id'];
}

$query = substr($query, 0, -1) . " FROM members" . $joins;
$result = mysql_query($query);
于 2013-01-03T01:15:29.253 回答
0

这基本上是数据的一个支点,但 MySQL 没有支点。因此,您可以使用与CASE此类似的聚合函数来复制功能:

select m.username as assignments,
  max(case when s.assign = 1 then s.grade end) Assign1,
  max(case when s.assign = 2 then s.grade end) Assign2,
  max(case when s.assign = 3 then s.grade end) Assign3,
  max(case when s.assign = 4 then s.grade end) Assign4,
  avg(s.grade) FinalGrade
from members m
left join subassign s
  on m.memberid = s.student
left join assignments a
  on s.assign = a.id
group by m.username

请参阅带有演示的 SQL Fiddle

结果:

| ASSIGNMENTS | ASSIGN1 | ASSIGN2 | ASSIGN3 | ASSIGN4 | FINALGRADE |
--------------------------------------------------------------------
|  username 1 |      50 |      68 |      32 |      68 |       54.5 |
|  username 2 |      75 |      86 |      89 |      36 |       71.5 |
|  username 3 |      99 |      70 |      24 |     100 |      73.25 |
于 2013-01-03T01:48:57.577 回答
0

不是 100% 确定我的查询是否正确,但你明白了:

SELECT 
    a.title, 
    s.grade, 
    m.Username 
FROM 
    members m
LEFT JOIN
    subassign AS s ON s.student = m.Memberid
LEFT JOIN
    assignments AS a ON a.id = s.assign
于 2013-01-03T01:15:43.300 回答