我有这个问题:
$result3 = mysql_query("SELECT posts.id, posts.date, posts.title, comments.post, comments.id, comments.date FROM posts, comments WHERE posts.id = comments.post")
or die(mysql_error());
while($row2 = mysql_fetch_array( $result3 )) {
echo $row2['title'];
}
Run Code Online (Sandbox Code Playgroud)
问题在于posts.id,posts.date和comments.id,comments.date.我怎么能得出$row2['....];我试过的两张桌子的id,日期$row2['posts.id'];但它没有用!
在查询中命名列(这称为列别名),如下所示:
SELECT
posts.id as postsID,
posts.date,
posts.title,
comments.post,
comments.id as CommentsID,
comments.date
FROM
jaut_posts,
f1_comments
WHERE
jaut_posts.id = f1_comments.post
Run Code Online (Sandbox Code Playgroud)
然后你可以使用:
echo $row2['postsID'];
echo $row2['commentsID'];
Run Code Online (Sandbox Code Playgroud)
编辑:
您也可以从我编写和回答的这个问题中受益,该问题讨论了许多常见的SQL查询和请求.
| 归档时间: |
|
| 查看次数: |
8765 次 |
| 最近记录: |