2

我准备了一个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(日期)对它们进行分组......

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

4

1 回答 1

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 表中的值。我想这就是你想要的。理解你想要做什么有点困难,因为我无法理解表名等。我希望这会有所帮助

ps 同样,您可以使用类似的连接来获取用户名而不是用户 ID 和标题,如果帖子 ID...

于 2013-08-02T13:53:49.577 回答