0

私がやろうとしているのは、カテゴリ ONCE とそのフォーラムのみを印刷することです。しかし、代わりに、カテゴリを数回印刷し、フォーラムで...

私のテーブル:

categories
id | name | disp_position

forums
id | cat_id | name | description | disp_position

コード:

$lastCatID = null;
$query = $db->query("SELECT f.id AS fid, f.cat_id, f.name AS forum_name, c.name AS cat_name FROM categories c 
                INNER JOIN forums f
                ORDER BY c.disp_position, c.id, f.disp_position");

<?php foreach($query as $row): ?>

<div class="catname">
<?php
if ($lastCatID != $row['cat_id']) {
echo '<h1>' . $row['cat_name'] . '</h1>';
$lastCatID = $row['cat_id'];
}
?>
</div>

<p><?=$row['forum_name']?></p>

<?php endforeach ?>

だから私は2つのカテゴリを持っています:

テスト カテゴリ (id 1) とテスト カテゴリ 2 (id 2)

およびこれらのフォーラム:

(1, 1, 'News & Announcements', 'Official announcements are posted here.', 1),
(2, 1, 'Proposals', 'Propose and help us improve.', 5),
(3, 2, 'Gameplay', 'Talk about the game here.', 3),
 (4, 2, 'Off Topic', 'Discuss all things not related to the game.', 4),
(5, 1, 'General', 'This forum is for general discussion.', 2),
(6, 1, 'Help & Support', 'Players helping players.', 6),
(7, 2, 'Bug Report', 'Found a bug? Help us squash it by reporting it here!', 7),
(8, 1, 'Trade', 'Sell your items or buy something you need.', 8);

出力:

Test category
News & Announcements
General
Test category
Gameplay
Off Topic
Test category
Proposals
Help & Support
Test category
Bug Report
Test category
Trade
News & Announcements
General
Test category 2
Gameplay
Off Topic
Test category 2
Proposals
Help & Support
Test category 2
Bug Report
Test category 2
Trade
4

1 に答える 1

0

結合に USING 句または WHERE 句がありません。基本的にあなたがする必要があります

INNER JOIN forums f ON c.id = f.cat_id

于 2013-07-20T15:24:19.527 に答える