1

我是 PHP 的初学者,但一直在论坛上尝试一个项目。我遇到了一个问题。

            $getLatest = dbquery("SELECT * FROM site_forum_thread_comments,site_forum_threads,site_forum_subs WHERE site_forum_thread_comments.thread_id=site_forum_threads.thread_id AND 
        site_forum_threads.sub_id=site_forum_subs.sub_id AND
        site_forum_subs.main_id='".$forum['ID']."'
        ORDER BY site_forum_thread_comments.timestamp DESC LIMIT 1");
            while ($latest = mysql_fetch_assoc($getLatest))
                {
                    $getUser = dbquery("SELECT * FROM users WHERE id='".$latest['user_comment_id']."'");
                    while ($user = mysql_fetch_assoc($getUser))
                    {
                            echo ' <BR>Latest Post: '.$latest['comment'].'
                            <BR>User: '.$user['username'].'
                            <BR>Forum: '.substr($latest['site_forum_threads.title'],0,20).'...';
                    }
                }

这只是代码片段。发生的情况是,$getLatest 从 SQL 查询中获取代码,这些代码来自我数据库中的许多不同表。我的问题:在论坛的最后一部分 '.substry($latest['...blahblahblah']

我可以在其中添加表格功能吗?所以我可以从 site_forum_threads 中检索列“标题”吗?因为在我的其他一个表中,还有一个名为“表”的列,并且查询在它应该从哪个列获取数据之间混淆了。

提前致谢!

编辑:@zerkms

$getLatest = dbquery("SELECT * FROM site_forum_thread_comments,site_forum_threads,site_forum_subs 
        site_forum_threads.title AS title_from_site_forum_threads 
        WHERE site_forum_thread_comments.thread_id=site_forum_threads.thread_id AND 
        site_forum_threads.sub_id=site_forum_subs.sub_id AND
        site_forum_subs.main_id='".$forum['ID']."' AND
        side_forum_threads.title= title_from_site_forum_threads
        ORDER BY site_forum_thread_comments.timestamp DESC LIMIT 1");
            while ($latest = mysql_fetch_assoc($getLatest))
                {
                    $getUser = dbquery("SELECT * FROM users WHERE id='".$latest['user_comment_id']."'");
                    while ($user = mysql_fetch_assoc($getUser))
                    {
                            echo ' <BR>Latest Post: '.$latest['comment'].'
                            <BR>User: '.$user['username'].'
                            <BR>Forum: '.substr($latest['title_from_site_forum_threads'],0,20).'...';
                    }
                }
4

1 回答 1

1

是的,只需明确指定名称并为其命名

site_forum_threads.title AS title_from_site_forum_threads

并使用$latest['title_from_site_forum_threads']

总共的选择部分可能看起来像

SELECT *, site_forum_threads.title AS title_from_site_forum_threads FROM ...

但我个人建议您始终只选择必要的东西,手动枚举用逗号分隔的所有字段

于 2012-06-03T03:35:12.817 回答