Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL查询将最后一条评论加入主题_Php_Mysql_Sql_Join_Sql Order By - Fatal编程技术网

Php MySQL查询将最后一条评论加入主题

Php MySQL查询将最后一条评论加入主题,php,mysql,sql,join,sql-order-by,Php,Mysql,Sql,Join,Sql Order By,我有一个主题和主题注释表。我想加入他们两个 +------------+ +------------+ | Topic | | Comments | +------------+ +------------+ | id | | parent_id | | title | | content | | createdate | | createdate | | content | | creator_id | +------------+ +--

我有一个主题和主题注释表。我想加入他们两个

+------------+ +------------+
| Topic      | | Comments   |
+------------+ +------------+
| id         | | parent_id  |
| title      | | content    |
| createdate | | createdate |
| content    | | creator_id |
+------------+ +------------+
连接位于
topic.id=topic\u comments.parent\u id
上。我想显示带有最新注释的主题,并按最新注释排序
createdate
。并且不显示重复的主题。有人能帮我吗

到目前为止,我有:

select p.id, p.title, p.createdate, p.content, p.int_0 as reacties_total, p.char_1 as prio, p.char_0 as status, r.createdate as r_createdate, r.creator_id as r_creator_id, r.content as r_content
from pages p, topic_reacties r
where r.parent_id = p.id
  and p.parent_id = ' . $this->id . ' 
order by p.int_2 desc

但是,这不会显示没有注释的主题。它只返回带有反应的主题。

是否显示带有最新评论并按评论排序的主题,还是显示每个主题(无论是否有评论)?这是两个不同的要求

SELECT title,content,max(createdate) as createdate 
FROM topic 
left join comments 
on topic.id=comments.parent_id 
group by title 
order by createdate desc;
如果您想显示带有最新注释的主题,请像使用join一样使用join(除了请丢失过时的语法),但要在
on
子句中添加更多细节:

select  p.id, p.title, p.createdate, p.content,
        p.int_0 as reacties_total, p.char_1 as prio,
        p.char_0 as status, r.createdate as r_createdate,
        r.creator_id as r_creator_id, r.content as r_content
from    pages p
join    topic_reacties r
    on  r.parent_id = p.id
    and r.createdate =(
        select  Max( createdate )
        from    topic_reacties
        where   parent_id = r.parent_id )
order by r.createdate;
不要让子查询吓到你。如果对
createdate
列进行了索引,您将看到令人印象深刻的效率

如果你想要所有的主题,不管它们是否有评论,但是那些只有最新评论的主题,那么使用外部连接。您仍然可以使用相同的子查询,但某些DBMS不允许在外部联接的
on
子句中使用子查询。比如甲骨文。我不确定MySQL。无论如何,为了安全起见,我总是将它移到
where
子句。(我处理许多不同的DBMS。)

但是,当您在
where
子句中检查外部联接的外部表时,可以将输出转换为与内部联接相同。所以你得耍点小把戏

select  p.id, p.title, p.createdate, p.content,
        p.int_0 as reacties_total, p.char_1 as prio,
        p.char_0 as status, r.createdate as r_createdate,
        r.creator_id as r_creator_id, r.content as r_content
from    pages p
left join topic_reacties r
    on  r.parent_id = p.id
where   (r.parent_id is null
    or  r.createdate =(
        select  Max( createdate )
        from    topic_reacties
        where   parent_id = r.parent_id ))
order by r.createdate;
请注意,我已经在
中的
复选框周围放置了括号/方括号,尽管如前所述,它们不是绝对必要的。但是,如果您添加任何其他检查,则需要将其置于括号之外

where   (r.parent_id is null
    or  r.createdate =(
        select  Max( createdate )
        from    topic_reacties
        where   parent_id = r.parent_id ))
    and p.ID = :SomeID

查看

Try LEFT OUTER JOIN主题\u reacties r ON p.id=r.parent\u id