Obtener temas mas actuales
Publicado por tulipo (1 intervención) el 31/07/2013 21:57:50
Hola a todos, que onda!
Me dispongo con una consulta sql, para conseguir los temas mas actuales de un simple foro, y tengo la siguiente disposicion:
Como puedo hacer la consulta para recuperar los mas recientes post, pero teniendo en cuenta los ultimos comentarios?
Con esto me da los ultimos comentarios
Y con esto los ultimos post,
Tengo que unir esas dos consultas y despues agruparlos por el id_post mas reciente(fecha)...
Alguien me echa una mano?? Gracias, y saluditos
Me dispongo con una consulta sql, para conseguir los temas mas actuales de un simple foro, y tengo la siguiente disposicion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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`);
Como puedo hacer la consulta para recuperar los mas recientes post, pero teniendo en cuenta los ultimos comentarios?
Con esto me da los ultimos comentarios
1
select id_post,id_user,contenido,fecha from f_comentario group by id_post order by fecha DESC
Y con esto los ultimos post,
1
select id_post,id_user,contenido,fecha from f_post order by fecha DESC
Tengo que unir esas dos consultas y despues agruparlos por el id_post mas reciente(fecha)...
Alguien me echa una mano?? Gracias, y saluditos
Valora esta pregunta


0