how to get value from mysql table ordered by another table?

╄→尐↘猪︶ㄣ 提交于 2020-02-05 08:56:11

问题


i have some reference to record for a given article. a reference might be of type an article, book, thesis etc.. each might have different attributes, e.g.

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

when i insert into one of the tables, i first into ref_type table with its type (book, article). then insert whichever table it belongs. (e.g. if it is an article, insert into article table)..

now, i have to be able list references in order.

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

this way it lists all thesis records then lists article table etc..

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

i know it is simply because of while loop, but how to get this (same order with ref_types table..)??

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

any help is appreciated. thanks in advance..


回答1:


In addition to having ReferenceType column in ref_types table, you also need a Reference_ID column that refers to the actual ID in the corresponding table as a 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

Then, you can avoid a WHILE loop and let MySQL do the work for you with JOINs:

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

Yields the result:

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



回答2:


Instead of printing the data right away, store it in an array based of the 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.....

that way the rows in the $data array will be in the order based on the id in the ref_thesis table.

That said, I think you should go back and think about the db schema you have. You should be able to get what you want with a few simple JOIN statements in a single query, rather than querying the db inside a while loop



来源:https://stackoverflow.com/questions/9618717/how-to-get-value-from-mysql-table-ordered-by-another-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!