2013-01-03 69 views
2

我一直在想盡一切辦法來完成我所需要的。我不是一個noob,但我堅持這一點。 我想從3個表格中創建一個「網格」數據。我希望它看起來像這樣:如何從Mysql連接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是id nu在作業表中的作業的更多 學生是成員表中的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中創建一個新行。有人可以請幫助。我即將把我的電腦扔出窗外。

+0

不知道,但也許它不是明智的做法是使用MySQL任何更多可能想要轉換爲MYSQLi – Sir

回答

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); 
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 
+0

好的,謝謝你讓他們現在正確列表,但它會爲每個任務創建一個新行。現在一個愚蠢的問題,我將如何做到這一點,佈局會像我想要的?我對這方面的數據並不熟悉 – Tattedman

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 with Demo

結果:

| 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 |