Question

I prepare a sql query to get the most current topics of a simple forum, and I have the following provision:

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`);

How I can make the query to retrieve the most recent post, but considering the last comments?

This gives me the latest comments

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

And with that the last post,

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

I have to join these two queries and then group them by the 10 most recent id_post (date) ...

Someone gives me a hand? Thank you, and greetings

Was it helpful?

Solution

Correct me if I am misunderstanding you but what you are looking for is the 10 latest id_post, id_user, contenido, fecha from the comment table based when it was last commented on rather than when the post itself was made?

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

This will give you the values stored inside the f_comentario table. I think this is what you want. It is a little difficult to understand what you trying to do because I can't understand the table names etc. I hope this helps

p.s. Again you can use similar joins to get the user name instead of the user id and the title instead if the post id...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top