获得10条最新消息

时间:2013-08-01 08:14:44

标签: mysql groupwise-maximum

我准备一个sql查询来获取一个简单论坛的最新主题,我有以下条款:

CREATE TABLE IF NOT EXISTS `f_categoria` (
  `id_categoria` int(4) NOT NULL,
  `name` varchar(20) NOT NULL,
  `desc` varchar(50) NOT NULL,
  `id_foro` int(3) NOT NULL,
  `estado` int(1) NOT NULL,
  `visibilidad` int(2) NOT NULL,
  PRIMARY KEY (`id_categoria`),
  KEY `id_categoria` (`id_categoria`),
  KEY `id_foro` (`id_foro`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



INSERT INTO `f_categoria` (`id_categoria`, `name`, `desc`, `id_foro`, `estado`, `visibilidad`) VALUES
(1, 'Programacion', 'Programacion', 1, 1, 1),
(2, 'Modelado', 'Modeladoen 3d', 1, 1, 1);

-- --------------------------------------------------------

CREATE TABLE IF NOT EXISTS `f_comentario` (
  `id_comentario` bigint(20) NOT NULL AUTO_INCREMENT,
  `id_post` bigint(20) NOT NULL,
  `id_user` bigint(20) NOT NULL,
  `contenido` longtext NOT NULL,
  `number` int(4) NOT NULL,
  `fecha` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id_comentario`),
  KEY `id_post` (`id_post`),
  KEY `id_user` (`id_user`),
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;



INSERT INTO `f_comentario` (`id_comentario`, `id_post`, `id_user`, `contenido`, `number`, `fecha`) VALUES
(1, 1, 1, 'Este es el primer comentario', 1, '2013-07-30 23:12:53'),
(2, 1, 1, 'Este es el comentario 2', 2, '2013-07-30 23:25:12'),
(3, 2, 1, 'cOMENTARIO EN EL POST2', 1, '2013-07-30 23:53:44');

-- --------------------------------------------------------



CREATE TABLE IF NOT EXISTS `f_foro` (
  `id_foro` int(5) NOT NULL,
  `name` varchar(15) NOT NULL,
  `desc` varchar(50) NOT NULL,
  `visibilidad` int(2) NOT NULL,
  PRIMARY KEY (`id_foro`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



INSERT INTO `f_foro` (`id_foro`, `name`, `desc`, `visibilidad`) VALUES
(1, 'Prueba', 'Foro de prueba', 1);

-- --------------------------------------------------------


CREATE TABLE IF NOT EXISTS `f_post` (
  `id_post` bigint(20) NOT NULL AUTO_INCREMENT,
  `id_user` bigint(20) NOT NULL,
  `titulo` varchar(50) NOT NULL,
  `contenido` longtext NOT NULL,
  `fecha` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `estado` int(1) NOT NULL,
  `id_categoria` int(4) NOT NULL,
  PRIMARY KEY (`id_post`),
  KEY `id_user` (`id_user`),
  KEY `id_categoria` (`id_categoria`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;



INSERT INTO `f_post` (`id_post`, `id_user`, `titulo`, `contenido`, `fecha`, `estado`, `id_categoria`) VALUES
(1, 1, '¡Hola!', 'mensaje de prueba', '2013-07-30 23:12:05', 1, 1),
(2, 1, 'post 2', 'es el post2', '2013-07-30 23:27:25', 1, 2),
(3, 1, 'post3', 'el post3', '2013-07-31 00:04:52', 1, 1);

-- --------------------------------------------------------



CREATE TABLE IF NOT EXISTS `f_user` (
  `id_user` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(15) NOT NULL,
  `mail` varchar(50) NOT NULL,
  `fecha` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id_user`),
  UNIQUE KEY `name` (`name`,`mail`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

--
-- Volcado de datos para la tabla `f_user`
--

INSERT INTO `f_user` (`id_user`, `name`, `mail`, `fecha`) VALUES
(1, 'tulipo', 'f673150@rmqkr.net', '2013-07-30 23:07:36');




ALTER TABLE `f_categoria`
  ADD CONSTRAINT `f_categoria_ibfk_1` FOREIGN KEY (`id_foro`) REFERENCES `f_foro` (`id_foro`);


ALTER TABLE `f_comentario`
  ADD CONSTRAINT `f_comentario_ibfk_2` FOREIGN KEY (`id_user`) REFERENCES `f_user` (`id_user`),
  ADD CONSTRAINT `f_comentario_ibfk_1` FOREIGN KEY (`id_post`) REFERENCES `f_post` (`id_post`);


ALTER TABLE `f_post`
  ADD CONSTRAINT `f_post_ibfk_2` FOREIGN KEY (`id_categoria`) REFERENCES `f_categoria` (`id_categoria`),
  ADD CONSTRAINT `f_post_ibfk_1` FOREIGN KEY (`id_user`) REFERENCES `f_user` (`id_user`);

如何让查询检索最新的帖子,但考虑到最后的评论?

这给了我最新的评论

select id_post,id_user,contenido,fecha from f_comentario  group by id_post order by fecha DESC

这是最后一篇文章,

select id_post,id_user,contenido,fecha from f_post order by fecha DESC

我必须加入这两个查询,然后按最近的10个id_post(日期)对它们进行分组......

有人帮我一把吗?谢谢你,问候

1 个答案:

答案 0 :(得分:0)

如果我误解你,请纠正我,但你要找的是10条最新的id_post,id_user,contenido,fecha来自评论表,这是基于上次评论时而不是帖子本身的时候?

select TOP 10 f_com.id_post, f_com.id_user, f_pos.contenido,  f_com.contenido, f_com.fecha 
from f_comentario f_com
inner join f_post f_pos
on  f_pos.id_post = f_com.id_post 
order by fecha DESC

这将为您提供存储在f_comentario表中的值。 我想这就是你想要的。 要理解你想要做什么有点困难,因为我无法理解表名等。 我希望这有帮助

P.S。再次,您可以使用类似的联接来获取用户名而不是用户ID和标题,如果帖子ID ...