我可以只使用 1 个 sql 查询吗?
showthread.php
// Get Topic subject etc
$threadID = isset($_GET['threadID']) ? intval($_GET['threadID']) : 0;
$result = mysql_query("SELECT * FROM topics WHERE id = $threadID");
// Fetch rows
$row = mysql_fetch_assoc($result);
$subject = htmlspecialchars($row['subject']);
echo '<h2>'.$subject.'</h2>';
// Get posts that belong to this topic!
$posts = mysql_query("SELECT * FROM posts INNER JOIN users ON users.id = posts.user_id WHERE posts.topic_id = $threadID");
// posts.....
while ($post = mysql_fetch_assoc($posts)) {
echo '<br>'.$post['message'].'';
}
最佳答案
根据我的理解,你应该能够从这样的东西中获得你需要的所有信息? (加上或减去任何缺失的列)
SELECT topics.subject, posts.message
FROM posts
INNER JOIN users ON users.id = posts.user_id
INNER JOIN topics ON topics.id = posts.topic_id
WHERE posts.topic_id = $threadID
关于php - 我该如何改进这段代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6259892/