2012-03-08 35 views
1

我有一些參考文獻的記錄。參考文獻可能是類型的文章,書籍,論文等等。每個可能具有不同的屬性,例如 。如何從另一個表中排序的mysql表中獲取值?

//article_refs table 
ID Article_ID Article_Title Author_Name ... 
1 1   title1  author1 
2 1   title2  author2 

//thesis_refs table 
ID Article_ID Thesis_Title Thesis_Publisher ... 
1 1   thesis1  publisher1 
2 1   thesis2  publisher2 

//ref_types table 
ID Article_ID ReferenceType 
1 1   book 
2 1   article 
3 1   article 
4 1   book 

當我插入到其中一個表中時,我首先進入類型爲(書,文章)的ref_type表。然後插入它所屬的表。 (例如,如果它是一篇文章,插入到文章表中)。

現在,我必須能夠按順序列出參考。

$sql=mysql_query("SELECT * FROM ref_types 
WHERE $article_ID=Article_ID ORDER BY ID ASC"); 
while($row = mysql_fetch_array($sql)){ 
    $counter=1; 

    if($row[2]=="thesis"){ 
     $sqlthesis=mysql_query("SELECT * FROM thesis_refs 
     WHERE $article_ID=Article_ID 
     ORDER BY ID ASC"); 
     while($thesis_row = mysql_fetch_array($sqlthesis)){ 
      echo "record $counter: "; 
      echo $row[2]; 
      echo ", "; 
      echo $thesis_row[2]; 
      echo "<BR>"; 
      $counter++ 
     } 
    }elseif..... 

這種方式,列出了所有的論文記錄然後列出文章表等。

record 1: book1 
record 2: book2 
record 3: article1 
record 4: article2 

我知道這是僅僅是因爲while循環,但如何得到這個(與ref_types表順序相同。 。)?

record 1: book1 
record 2: article1 
record 3: article2 
record 4: book2 

任何幫助表示讚賞。 在此先感謝..

+0

請出示ref_thesis表也和表格之間的關鍵關係。 – 2012-03-08 14:05:35

+0

嗨馬庫斯,它具有與上述相同的結構,文章和書桌.. 我編輯了這個問題.. – teutara 2012-03-08 14:07:29

+0

我明白了。然後我會解決你的錯別字。 – 2012-03-08 14:21:54

回答

1

除了在ref_types表中具有ReferenceType列之外,還需要一個Reference_ID列指對應表中的實際ID爲foreign key

//ref_types table 
ID Article_ID ReferenceType Reference_ID 
1 1   book   1 
2 1   article  1 
3 1   article  2 
4 1   book   2 

然後,您可以避開WHILE循環,讓MySQL的做的工作爲你連接:

SELECT CONCAT('record ', rt.ID, ': ', 
    COALESCE(ar.Article_Title, tr.Thesis_Title, br.Book_Title)) 
FROM ref_types rt 
LEFT JOIN article_refs ar 
    ON rt.ReferenceType = 'article' AND ar.ID = rt.Reference_ID 
LEFT JOIN book_refs br 
    ON rt.ReferenceType = 'book' AND br.ID = rt.Reference_ID 
LEFT JOIN thesis_refs tr 
    ON rt.ReferenceType = 'thesis' AND tr.ID = rt.Reference_ID 

生成結果:

record 1: book1 
record 2: article1 
record 3: article2 
record 4: book2 
+0

@teutara,例如,當插入一本書的第1條。你插入書book_refs,它會產生一個新的ID,使用auto_increment。然後,當您將新記錄插入到ref_types中時,將爲ReferenceType插入「book」,併爲reference_ID插入來自book_refs的新ID。 – 2012-03-08 14:43:08

+0

感謝Marcus,但我不明白我將如何在Reference_ID列中擁有兩個1和兩個2實例?不應該是1,2,3,4? – teutara 2012-03-08 14:48:50

+0

不,Reference_ID不是auto_increment。您將ref_types.Reference_ID設置爲等於來自其他表(book_refs.ID,thesis_refs.ID或article_refs.ID)的ID值。 – 2012-03-08 14:51:05

0

而不是馬上打印數據,將其存儲在基於ref_thesis id的數組中。 e.g

$data = array(); 

$sql=mysql_query("SELECT * FROM reftypes 
WHERE $article_ID=Article_ID ORDER BY ID ASC"); 
while($row = mysql_fetch_array($sql)){ 
$counter=1; 

if($row[2]=="thesis"){ 
    $sqlthesis=mysql_query("SELECT * FROM ref_thesis 
    WHERE $article_ID=Article_ID 
    ORDER BY ID ASC"); 
    while($thesis_row = mysql_fetch_array($sqlthesis)){ 
     $data[ $row['id'] ] = $row; 
    } 
}elseif..... 

這種方式,$data陣列中的行會在基於ref_thesisid的順序。

這就是說,我認爲你應該回頭想想你擁有的數據庫模式。在單個查詢中,您應該能夠通過幾條簡單的JOIN語句獲得所需內容,而不是在while循環內查詢數據庫

+0

謝謝scibuff ..將存儲在一個數組中幫助我下單嗎?我無法捕捉它..順便說一句,我是你理解一個新手.. – teutara 2012-03-08 14:11:30

相關問題