我无法从两个不同的表格中获取3条最新评论。
这是我的代码,它与一个内连接完美配合:
$query = mysql_query("SELECT COUNT(c.topic_id) AS ctid, COUNT(c.deck_id) AS dtid, f.id AS forumid, f.class AS forumclass, f.name AS forumname, f.url AS forumurl,
c.id AS commentid, c.user_id AS commentuser, c.user_name AS commentusername, c.date AS commentdate,
c.topic_id AS topicid, c.deck_id AS deckid
FROM ".$prefix."comment AS c
INNER JOIN ".$prefix."forum AS f
ON c.topic_id = f.id GROUP BY f.id
ORDER BY commentdate DESC LIMIT 3") or die(mysql_error());
这很好,它显示了论坛表中的3条最新评论,但是我也在甲板表中有评论,但是当我在查询中添加另一个JOIN时,它就不再工作了。
$query = mysql_query("SELECT COUNT(c.topic_id) AS ctid, COUNT(c.deck_id) AS dtid, f.id AS forumid, f.class AS forumclass, f.name AS forumname, f.url AS forumurl,
c.id AS commentid, c.user_id AS commentuser, c.user_name AS commentusername, c.date AS commentdate,
c.topic_id AS topicid, c.deck_id AS deckid, , d.id, d.url AS deckurl, d.name AS deckname
FROM ".$prefix."comment AS c
INNER JOIN ".$prefix."forum AS f
ON c.topic_id = f.id
INNER JOIN ".$prefix."decks AS d
ON c.deck_id = d.id
GROUP BY f.id ORDER BY commentdate DESC LIMIT 3") or die(mysql_error());
有一个评论表,在评论表中有一个topic_id列,它等于论坛表的id列,还有一个deck_id列,它等于甲板table的id列。
显然GROUP BY f.id对两个内部联接并不好。
在此查询之后我有了($ top = mysql_fetch_assoc($ query)){...然后if($ top [' deckid'] == 0)然后打印topicid信息else print甲板上的信息。
编辑: 评论表(现在只对我们很重要): id,topic_id,deck_id
topic_id =论坛表格的ID
deck_id = deck table' id
显然,每个论坛主题都没有评论。
例如:论坛ID 5有4条评论,然后是评论表,例如:ID 1,2,3,4有topic_id 4,4,4,4和deck_id 0,0,0,0。
如果没有评论,则评论表中没有任何内容。所以论坛ID 6有0条评论,然后评论表中没有任何内容。
如果牌组12有2条评论,那么评论表例如:5,6有deck_id 12,12和topic_id 0,0。
论坛表: ID
甲板表: ID
EDIT2:解决方案(不太好,但有效):
//count how many comments the latest 3 deck topic has
$new_comment_query = mysql_query("SELECT COUNT(c.deck_id) AS dtid, c.id, c.deck_id, c.date, d.id
FROM ".$prefix."comment AS c LEFT JOIN ".$prefix."decks AS d ON d.id = c.deck_id GROUP BY d.id ORDER BY date DESC LIMIT 3");
$new_one = mysql_fetch_array($new_comment_query);
//count how many comments the latest 3 forum topic has
$new_forum_query = mysql_query("SELECT COUNT(c.topic_id) AS ctid, c.id, c.topic_id, c.date, f.id
FROM ".$prefix."comment AS c LEFT JOIN ".$prefix."forum AS f ON f.id = c.topic_id GROUP BY f.id ORDER BY date DESC LIMIT 3");
$newer_one = mysql_fetch_array($new_forum_query);
//get all the comments
$comment_query = mysql_query("SELECT id, topic_id, deck_id, date FROM ".$prefix."comment ORDER BY date DESC LIMIT 3");
while ($comment = mysql_fetch_assoc($comment_query))
{
if($comment['topic_id']==0)
{
$deck_query = mysql_query("SELECT * FROM ".$prefix."decks WHERE id=".$comment['deck_id']);
while ($deck_comments = mysql_fetch_assoc($deck_query))
{
//print all the things!
}
}
elseif($comment['deck_id']==0)
{
$forum_query = mysql_query("SELECT * FROM ".$prefix."forum WHERE id=".$comment['topic_id']);
while ($forum_comments = mysql_fetch_assoc($forum_query))
{
//print all the things!
}
}
}
答案 0 :(得分:3)
尝试运行此查询:
SELECT * FROM
(
SELECT COUNT(c.topic_id) AS ctid, COUNT(c.deck_id) AS dtid,
f.id AS forumid, f.class AS forumclass, f.name AS forumname,
f.url AS forumurl,
c.id AS commentid, c.user_id AS commentuser, c.user_name AS commentusername,
c.date AS commentdate, c.topic_id AS topicid, c.deck_id AS deckid,
d.id, d.url AS deckurl, d.name AS deckname
FROM ".$prefix."comment AS c
INNER JOIN ".$prefix."forum AS f
ON c.topic_id = f.id
INNER JOIN ".$prefix."decks AS d
ON c.deck_id = d.id
ORDER BY commentdate DESC
) t1
GROUP BY t1.forumid
LIMIT 3
我首先执行您的复杂JOIN
而不导致问题的GROUP BY
。然后我SELECT
来自该临时表的所有内容,按forumid
进行分组。
老实说,我很惊讶你的服务器没有对你的第一个查询进行抨击,更不用说第二个问题,但希望我的回答可以帮助你。
答案 1 :(得分:0)
//count how many comments the latest 3 deck topic has
$new_comment_query = mysql_query("SELECT COUNT(c.deck_id) AS dtid, c.id, c.deck_id, c.date, d.id
FROM ".$prefix."comment AS c LEFT JOIN ".$prefix."decks AS d ON d.id = c.deck_id GROUP BY d.id ORDER BY date DESC LIMIT 3");
$new_one = mysql_fetch_array($new_comment_query);
//count how many comments the latest 3 forum topic has
$new_forum_query = mysql_query("SELECT COUNT(c.topic_id) AS ctid, c.id, c.topic_id, c.date, f.id
FROM ".$prefix."comment AS c LEFT JOIN ".$prefix."forum AS f ON f.id = c.topic_id GROUP BY f.id ORDER BY date DESC LIMIT 3");
$newer_one = mysql_fetch_array($new_forum_query);
//get all the comments
$comment_query = mysql_query("SELECT id, topic_id, deck_id, date FROM ".$prefix."comment ORDER BY date DESC LIMIT 3");
while ($comment = mysql_fetch_assoc($comment_query))
{
if($comment['topic_id']==0)
{
$deck_query = mysql_query("SELECT * FROM ".$prefix."decks WHERE id=".$comment['deck_id']);
while ($deck_comments = mysql_fetch_assoc($deck_query))
{
//print all the things!
}
}
elseif($comment['deck_id']==0)
{
$forum_query = mysql_query("SELECT * FROM ".$prefix."forum WHERE id=".$comment['topic_id']);
while ($forum_comments = mysql_fetch_assoc($forum_query))
{
//print all the things!
}
}
}