Tarihe dayanan en son mesajı alıyorum

3 Cevap php

Nasıl ben iki tablo var olduğunda en son mesajı görüntüleniyor, hem creation_date adında bir sütun içeren yaklaşık gidiyor

This would be simple if all I had to do was get the most recent post based on posts created_on value however if a post contains replies I need to factor this into the equation. If a post has a more recent reply I want to get the replies created_on value but also get the posts post_id and subject.

posts tablo yapısı:

CREATE TABLE `posts` (
  `post_id` bigint(20) unsigned NOT NULL auto_increment,
  `cat_id` bigint(20) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `subject` tinytext NOT NULL,
  `comments` text NOT NULL,
  `created_on` datetime NOT NULL,
  `status` varchar(10) NOT NULL default 'INACTIVE',
  `private_post` varchar(10) NOT NULL default 'PUBLIC',
  `db_location` varchar(10) NOT NULL,
  PRIMARY KEY  (`post_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

replies tablo yapısı:

CREATE TABLE `replies` (
  `reply_id` bigint(20) unsigned NOT NULL auto_increment,
  `post_id` bigint(20) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `comments` text NOT NULL,
  `created_on` datetime NOT NULL,
  `notify` varchar(5) NOT NULL default 'YES',
  `status` varchar(10) NOT NULL default 'INACTIVE',
  `db_location` varchar(10) NOT NULL,
  PRIMARY KEY  (`reply_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

İşte benim sorgu şimdiye kadar. Ben tarihleri ​​çıkarma benim girişimi kaldırdık.

$strQuery = "SELECT posts.post_id, posts.created_on, replies.created_on, posts.subject ";
$strQuery = $strQuery."FROM posts ,replies ";
$strQuery = $strQuery."WHERE posts.post_id = replies.post_id ";
$strQuery = $strQuery."AND posts.cat_id = '".$row->cat_id."'";

3 Cevap

$strQuery = "
  SELECT posts.post_id, GREATEST(posts.created_on, replies.created_on)  AS latestDate, posts.subject
  FROM posts, replies
  WHERE posts.post_id = replies.post_id
  AND posts.cat_id = {$row->cat_id}
  GROUP BY posts.post_id
  ORDER BY latestDate DESC;
";

GÜNCELLEME: henüz herhangi bir yanıt yok bu mesajları dahil olmayacak gibi görünüyor ikinci günü, yukarıda, aslında yanlıştır. Bunu yapmak için daha doğru bir yoldur:

$strQuery = "
  SELECT posts.post_id, GREATEST(posts.created_on, replies.created_on) AS latestDate,
  FROM posts
  LEFT JOIN replies ON (posts.post_id = replies.post_id)
  WHERE posts.cat_id = {$row->cat_id}
  GROUP BY posts.post_id
  ORDER BY latestDate DESC
  LIMIT 0,1;
";

SEÇ posts.post_id, posts.subject, replies.post_id

FROM posts LEFT JOIN replies ON post.post_id = replies.post_id

WHERE posts.cat_id = '$ row-> cat_id'

Posts.post_id DESC ORDER BY DESC replies.post_id

Hiçbir cevap var, bir NULL döner ve size çıktı PHP kullanarak filtre edebilirsiniz.

En son mesaj alın, bir mesaja en son cevap, sonra onları en son bir olsun.

This uses order by created_on desc limit 1 to only get the last item from a table. I'd suggest adding keys on created_on columns for both tables, and for post_id column in replies table.

Güncelleme: Ayrıca cat_id indeksleme gerekiyor.

select * from (
select * from 
(
select p.post_id, p.created_on , 'post' as post_type
from posts p
where p.cat_id = '$row->cat_id'
order by p.created_on  desc limit 1
)post
union 
select * from 
(
select p.post_id, r.created_on , 'reply'
from posts p
inner join replies r on r.post_id = p.post_id
where p.cat_id = '$row->cat_id'
order by r.created_on  desc limit 1
)reply
)big order by big.created_on limit 1;