从JOIN查询获取输出的问题

时间:2015-08-16 02:19:27

标签: php mysql sql join count

我正在尝试加入,如果结构正确,我不是肯定的,但它并没有失败。我正在处理的查询$query2。我正在尝试SELECT匹配form_categories表的id等于forum_topics表中的category_id并计算其数量。基本上如果某个类别中有10个不同的主题,我希望显示10个。

我正在尝试输出信息的地方正在死亡。它只是从我的页面消失了。

如果我的查询在我正在尝试的内容中是正确的,那么这是输出失败...

<?php $numrows2 = mysqli_num_rows($query2);?>
<?php if ($numrows2 >= 0) : ?>
    <?php while ($row2 = mysqli_fetch_assoc($query2)) : ?>
        <?php $threads = $row['tid2']; ?>
        <div class="discussions_right">
        <p>Threads<?php echo $threads; ?></p>
    <?php endwhile; ?>
<?php endif; ?> 

完整代码

$query = mysqli_query($con,"SELECT * FROM forum_categories ORDER BY category_section ASC, category_title ASC")
or die ("Query failed: %s\n".($query->error));
$query2 = mysqli_query($con,"SELECT t.*, COUNT(c.id) AS tid FROM forum_topics AS t JOIN forum_categories AS c")
or die ("Query2 failed: %s\n".($query2->error));
?>
<?php if(mysqli_num_rows($query) > 0): ?>
<div class="discussions_table">
    <?php while($row = mysqli_fetch_assoc($query)): ?>
        <?php if($row['category_section'] !== $category_section): ?>
            <?php $category_section = $row['category_section'] ?>

            <div class="category_section">
                <?= $category_section ?>
            </div>
        <?php endif; ?>         
        <div class="category_border">
            <div class="discussions_left">
                <div class="discussions_category_title">
                    <a href="forum_view_category.php?cid=<?= $row['id'] ?>">
                        <?= $row['category_title'] ?>
                    </a>
                </div>
                <div class="discussions_category_description">
                    <?= $row['category_description'] ?>
                </div>
            </div>
            <?php $numrows2 = mysqli_num_rows($query2);?>
            <?php if($numrows2 >= 0):?>
                <?php while($row2 = mysqli_fetch_assoc($query2)): ?>
                    <?php $threads = $row['tid2']; ?>

            <div class="discussions_right">
                <p>Threads<?php echo $threads; ?></p>
                <?php endwhile; ?>
            <?php endif; ?> 
                <p>Posts</p>
            </div>
        </div>      
    <?php endwhile; ?>

<?php else: ?>
    <p>There are no posted categories available yet.</p>
<?php endif; ?>

我做错了什么?

更新:显示表格结构。

CREATE TABLE `forum_categories` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `category_section` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `category_title` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
 `category_description` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `last_post_date` datetime DEFAULT NULL,
 `last_user_posted` int(11) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci


forum_topics
CREATE TABLE `forum_topics` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `category_id` int(11) NOT NULL,
 `topic_title` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
 `topic_creator` int(11) NOT NULL,
 `topic_last_user` int(11) DEFAULT NULL,
 `topic_date` datetime NOT NULL,
 `topic_reply_date` datetime NOT NULL,
 `topic_views` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

1 个答案:

答案 0 :(得分:1)

您是否测试了此查询?

SELECT t.*, COUNT(c.id) AS tid FROM forum_topics AS t JOIN forum_categories AS c

我认为这不符合你的期望。我可能错了。

编辑:

尝试此查询

SELECT t.*, COUNT(c.id) AS tid FROM forum_topics AS t INNER JOIN forum_categories AS c ON t.category_id = c.id