我用两个单独的查询从 mysql 数据库中读取了一篇文章及其评论
$result = mysql_query("SELECT * FROM articles WHERE article_id='$id'");
$row = mysql_fetch_array($result);
$title=$row['title'];
........
和
$result = mysql_query("SELECT * FROM comments WHERE article_id='$id'");
while($row = mysql_fetch_array($result)) {
$comment_title=$row['title'];
.........
}
从数据库中读取这组数据的最佳方式是什么?或者是否可以通过一次查询或一次交易来捕获数据?
注意:我的问题是第一次查询文章只针对一行;但第二个需要一个循环来处理(并以 html 格式显示)多个评论。
$result = mysql_query("SELECT articles.title as article_title, comments.title as comment_title FROM articles LEFT JOIN comments ON articles.article_id = comments.article_id WHERE articles.article_id = '$id'");
$row = mysql_fetch_array($result);
$article_title=$row['article_title'];
$comment_title=$row['comment_title '];
我是一名优秀的程序员,十分优秀!