Skip to main content
se añadieron 87 caracteres en el cuerpo
Origen Enlace
Josee Naava
  • 147
  • 1
  • 10

alguien podria ayudarme a terminar la consulta y obtener el resultado deseado?

alguien podria ayudarme a terminar la consulta y obtener el resultado deseado?

se añadieron 495 caracteres en el cuerpo
Origen Enlace
Josee Naava
  • 147
  • 1
  • 10
CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL,
`titulo` varchar(200) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE         CURRENT_TIMESTAMP
 );

INSERT INTO `post` (`id`, `titulo`, `user_id`, `created_at`) VALUES
(1, 'group by mysql', 19, '2018-02-18 19:10:30');

CREATE TABLE IF NOT EXISTS `comentarios` (
`id` int(11) NOT NULL,
`content` varchar(200) NOT NULL,
`post_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 );

INSERT INTO `comentarios` (`id`, `content`, `post_id`, `user_id`,    `created_at`) VALUES
(1, 'comentario 1', 1, 19, '2018-02-19 13:12:09'),
(2, 'comentario 2', 1, 20, '2018-02-20 23:42:09'),
(3, 'comentario 3', 1, 19, '2018-02-21 19:12:30'),
(4, 'comentario 4', 1, 21, '2018-02-26 11:38:34'),
(5, 'comentario 5', 1, 22, '2018-02-28 19:13:15');

CREATE TABLE IF NOT EXISTS `likes` (
`id` int(11) NOT NULL,
`post_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 );

INSERT INTO `likes` (`id`, `post_id`, `user_id`, `created_at`) VALUES
(1, 1, 19, '2018-02-28 19:16:12'),
(2, 1, 21, '2018-02-28 19:16:12'),
(3, 1, 22, '2018-02-2822 19:16:21');

EDIT

He avanzado en la consulta, aun me falta lograr que si hay likes en una fecha que no hay comentarios los comentarios y el contador de los comentarios salga en null o 0. de igual forma si hay un comentario en el fecha que no hay likes que los likes salgan en null o 0.

consulta actual

SELECT SELECT
  p.id as post_id,
  titulo,
  MAX(DATE(p.created_at)) as fecha_post,
  c.content,
  MAX(DATE(c.created_at)) as fecha_comentario,
  IFNULL(countCOUNT(c.user_id), null) as count_personas,
  IFNULLMAX(countDATE(l.user_idcreated_at), null) as count_likesfecha_likes,
  COUNT(l.created_atuser_id) as fecha_likescount_likes
  FROM post as p
  LEFT OUTER JOIN comentarios as c 
       ON p.id=c.post_id 
  LEFT OUTER JOIN likes as l
       ON p.id=l.post_id and date(c.created_at)=date(l.created_at)
  GROUP BY p.id, DATE(p.created_at), DATE(c.created_at), DATE(l.created_at;created_at); 

update campos

UPDATE  likes
SET created_at='2018-02-22 14:46:21'
WHERE id=3

salida que espero

introducir la descripción de la imagen aquíintroducir la descripción de la imagen aquí

CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL,
`titulo` varchar(200) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE         CURRENT_TIMESTAMP
 );

INSERT INTO `post` (`id`, `titulo`, `user_id`, `created_at`) VALUES
(1, 'group by mysql', 19, '2018-02-18 19:10:30');

CREATE TABLE IF NOT EXISTS `comentarios` (
`id` int(11) NOT NULL,
`content` varchar(200) NOT NULL,
`post_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 );

INSERT INTO `comentarios` (`id`, `content`, `post_id`, `user_id`,    `created_at`) VALUES
(1, 'comentario 1', 1, 19, '2018-02-19 13:12:09'),
(2, 'comentario 2', 1, 20, '2018-02-20 23:42:09'),
(3, 'comentario 3', 1, 19, '2018-02-21 19:12:30'),
(4, 'comentario 4', 1, 21, '2018-02-26 11:38:34'),
(5, 'comentario 5', 1, 22, '2018-02-28 19:13:15');

CREATE TABLE IF NOT EXISTS `likes` (
`id` int(11) NOT NULL,
`post_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 );

INSERT INTO `likes` (`id`, `post_id`, `user_id`, `created_at`) VALUES
(1, 1, 19, '2018-02-28 19:16:12'),
(2, 1, 21, '2018-02-28 19:16:12'),
(3, 1, 22, '2018-02-28 19:16:21');

consulta

SELECT 
  p.id as post_id,
  titulo,
  p.created_at as fecha_post,
  c.content,
  c.created_at as fecha_comentario,
  IFNULL(count(c.user_id), null) as count_personas,
  IFNULL(count(l.user_id), null) as count_likes,
  l.created_at as fecha_likes
FROM post as p
LEFT JOIN comentarios as c 
       ON p.id=c.post_id
LEFT JOIN likes as l
       ON p.id=l.post_id
GROUP BY c.created_at,l.created_at;

salida que espero

introducir la descripción de la imagen aquí

CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL,
`titulo` varchar(200) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE         CURRENT_TIMESTAMP
 );

INSERT INTO `post` (`id`, `titulo`, `user_id`, `created_at`) VALUES
(1, 'group by mysql', 19, '2018-02-18 19:10:30');

CREATE TABLE IF NOT EXISTS `comentarios` (
`id` int(11) NOT NULL,
`content` varchar(200) NOT NULL,
`post_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 );

INSERT INTO `comentarios` (`id`, `content`, `post_id`, `user_id`,    `created_at`) VALUES
(1, 'comentario 1', 1, 19, '2018-02-19 13:12:09'),
(2, 'comentario 2', 1, 20, '2018-02-20 23:42:09'),
(3, 'comentario 3', 1, 19, '2018-02-21 19:12:30'),
(4, 'comentario 4', 1, 21, '2018-02-26 11:38:34'),
(5, 'comentario 5', 1, 22, '2018-02-28 19:13:15');

CREATE TABLE IF NOT EXISTS `likes` (
`id` int(11) NOT NULL,
`post_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
 );

INSERT INTO `likes` (`id`, `post_id`, `user_id`, `created_at`) VALUES
(1, 1, 19, '2018-02-28 19:16:12'),
(2, 1, 21, '2018-02-28 19:16:12'),
(3, 1, 22, '2018-02-22 19:16:21');

EDIT

He avanzado en la consulta, aun me falta lograr que si hay likes en una fecha que no hay comentarios los comentarios y el contador de los comentarios salga en null o 0. de igual forma si hay un comentario en el fecha que no hay likes que los likes salgan en null o 0.

consulta actual

 SELECT
 p.id as post_id,
 titulo,
 MAX(DATE(p.created_at)) as fecha_post,
 MAX(DATE(c.created_at)) as fecha_comentario,
 COUNT(c.user_id) as count_personas,
 MAX(DATE(l.created_at)) as fecha_likes,
COUNT(l.user_id) as count_likes
  FROM post as p
  LEFT OUTER JOIN comentarios as c 
   ON p.id=c.post_id 
  LEFT OUTER JOIN likes as l
    ON p.id=l.post_id and date(c.created_at)=date(l.created_at)
  GROUP BY p.id, DATE(p.created_at), DATE(c.created_at), DATE(l.created_at); 

update campos

UPDATE  likes
SET created_at='2018-02-22 14:46:21'
WHERE id=3

salida que espero

introducir la descripción de la imagen aquí

se eliminó 1 carácter en el cuerpo
Origen Enlace
Josee Naava
  • 147
  • 1
  • 10

**salida que espero **salida que espero

introducir la descripción de la imagen aquíintroducir la descripción de la imagen aquí

**salida que espero **

introducir la descripción de la imagen aquí

salida que espero

introducir la descripción de la imagen aquí

se añadieron 681 caracteres en el cuerpo
Origen Enlace
Josee Naava
  • 147
  • 1
  • 10
Loading
Origen Enlace
Josee Naava
  • 147
  • 1
  • 10
Loading