1

我有两个 SQL 表:

  • 消息
  • 编辑

在新闻表中,我有 2 个列(News_Added 和 News_Updated)。我正在将编辑器 ID 输入到这些列并将它们加入编辑器表的 Ed_ID 以获取编辑器名称。

由于我有 2 个 while 查询,我必须使用 2 个 while 循环,但在 1 个 html 表中。我不知道如何解决这个问题。有什么帮助吗?

echo '<table border="0">
<tr><th>News ID</th><th>Title</th><th>Added By</th><th>Last Edit By</th><th>Action</th</tr>';

$query = mysql_query("SELECT * FROM N1News AS N1
LEFT JOIN A1Editors AS A1 ON N1.N1_Addedby=A1.Ed_ID WHERE N1_Co=$Ed_Co ORDER BY N1_ID LIMIT $show,$limit");
    while($row = mysql_fetch_array($query)){
    echo '<tr><td align="center">'.$row["N1_ID"].'</td><td><a href="news_edit2.php?n_id='.$row["N1_ID"].'">'.$row["N1_Title"].'</a></td>
    <td><a href="news_edit2.php?n_id='.$row['N1_ID'].'"><img src="images/icons/hammer_screwdriver.png" alt="Edit News" /></a><a href="news_del.php?n_id='.$row["N1_ID"].'"><img src="images/icons/cross.png" alt="Delete News" /></a></td><td>'.$row["Ed_Name"].'</td>';
}
$query7 = mysql_query("SELECT * FROM N1News AS N1
LEFT JOIN A1Editors AS A1 ON N1.N1_Lastedit=A1.Ed_ID WHERE N1_Co=$Ed_Co ORDER BY N1_ID LIMIT $show,$limit");
    while($row7 = mysql_fetch_array($query7)){
    echo '<td>'.$row7["Ed_Name"].'</td>';
    } 
4

3 回答 3

1

可能有更有效的方法来组合您的两个查询,但一个快速的解决方案是使用UNION

$query = "(SELECT * FROM N1News AS N1
            LEFT JOIN A1Editors AS A1 ON N1.N1_Addedby=A1.Ed_ID WHERE N1_Co=$Ed_Co ORDER BY N1_ID LIMIT $show,$limit)
          UNION
          (SELECT * FROM N1News AS N2
            LEFT JOIN A1Editors AS A2 ON N2.N1_Lastedit=A2.Ed_ID WHERE N1_Co=$Ed_Co ORDER BY N1_ID LIMIT $show,$limit)
          ORDER BY N1.N1_ID

这样一来,您就可以将所有结果放在一个数组中,从而可以轻松生成表格。

顺便说一句,这些mysql_*函数已被弃用,您应该真正切换到 PDO 或 mysqli 中的准备好的语句

于 2012-11-09T20:38:53.523 回答
1

这里的解决方案是创建一个一次检索所有信息的查询,就像这样

SELECT N1.*, A1.ED_name addedby_name, A2.ED_name lasteditor_name FROM N1News AS N1
INNER JOIN A1Editors AS A1 ON N1.N1_Addedby=A1.Ed_ID
LEFT JOIN A1Editors AS A2 ON N1.N1_Lastedit=A2.Ed_ID
WHERE N1_Co=$Ed_Co
ORDER BY N1_ID
LIMIT $show,$limit

请注意,此查询假定每个新闻项都引用一个 AdditionalBy 编辑器。在此查询中,对 LastEdit 的引用是可选的。

于 2012-11-09T20:56:31.147 回答
0

您可以将第二个查询放入第一个 while。

<?php
$query = mysql_query("SELECT * FROM N1News AS N1
LEFT JOIN A1Editors AS A1 ON N1.N1_Addedby=A1.Ed_ID WHERE N1_Co=$Ed_Co ORDER BY N1_ID LIMIT $show,$limit");
while($row = mysql_fetch_array($query)){
echo '<tr><td align="center">'.$row["N1_ID"].'</td><td><a href="news_edit2.php?n_id='.$row["N1_ID"].'">'.$row["N1_Title"].'</a></td>
    <td><a href="news_edit2.php?n_id='.$row['N1_ID'].'"><img src="images/icons/hammer_screwdriver.png" alt="Edit News" /></a><a href="news_del.php?n_id='.$row["N1_ID"].'"><img src="images/icons/cross.png" alt="Delete News" /></a></td><td>'.$row["Ed_Name"].'</td>';
    // call second query //
    $newsid = $row["N1_ID"];

    $query7 = mysql_query("SELECT * FROM N1News AS N1
    LEFT JOIN A1Editors AS A1 ON N1.N1_Lastedit=A1.Ed_ID WHERE N1_ID = ".$newsid." AND N1_Co=$Ed_Co ORDER BY N1_ID LIMIT $show,$limit");

    if($row7 = mysql_fetch_array($query7)){
    echo '<td>'.$row7["Ed_Name"].'</td>';
    }
    /////////////////////

    }

?>
于 2012-11-09T20:38:28.127 回答