2

Tengo 3 tablas y necesito listar todas las opciones junto con todos los post que tienen o no tienen opciones, actualmente lo he realizado, pero me esta mostrando únicamente los post que tienen opciones pero los que no tienen opciones no me los muestra en el listado.

El ejemplo en sqlfiddle: http://sqlfiddle.com/#!9/8d27dd/1

Si se fijan en el listado, el post con ID 1, tiene 3 opciones que se le asignaron y me muestra una cuarta fila valor null, y el post con ID 2, tiene una opción asignada, pero necesito mostrar todas las otras filas con valores nulos de todas las opciones existentes en la tabla[post_options].

Tablas:

tabla[ post ] - Guarda todos los post principales

tabla[ post_options] - Guarda todas las opciones de los post

tabla[ post_has_options] - Guarda todos los post que tienen opciones

Código de las tablas:

CREATE TABLE `post` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `id_post_type` tinyint(3) unsigned NOT NULL,
  `title` varchar(255) NOT NULL,
  `create_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `FK_title_UNIQUE` (`title`) USING BTREE,
  KEY `FK_post_post_types` (`id_post_type`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

INSERT INTO `post` VALUES ('1', '1', 'Title 1', '2018-01-27 14:58:24', '2018-01-27 23:10:00');
INSERT INTO `post` VALUES ('2', '1', 'Title 2', '2018-01-27 14:58:24', '2018-01-27 23:10:00');
INSERT INTO `post` VALUES ('3', '1', 'Title 3', '2018-01-27 14:58:24', '2018-01-27 23:10:00');
INSERT INTO `post` VALUES ('4', '1', 'Title 4', '2018-01-27 14:58:24', '2018-01-27 23:10:00');

CREATE TABLE `post_has_options` (
  `id_post` int(10) unsigned NOT NULL,
  `id_post_work_type` tinyint(3) unsigned NOT NULL,
  `create_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id_post`,`id_post_work_type`),
  KEY `id_post_work_type` (`id_post_work_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `post_has_options` VALUES ('1', '1', '2018-01-27 22:00:51', '2018-01-27 22:00:51');
INSERT INTO `post_has_options` VALUES ('1', '2', '2018-01-27 22:00:54', '2018-01-27 22:00:54');
INSERT INTO `post_has_options` VALUES ('1', '3', '2018-01-27 22:00:58', '2018-01-27 22:00:58');
INSERT INTO `post_has_options` VALUES ('2', '2', '2018-01-27 22:45:19', '2018-01-27 22:45:19');

CREATE TABLE `post_options` (
  `id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `create_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `FK_name_UNIQUE` (`name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

INSERT INTO `post_options` VALUES ('1', 'Work type 1', '2018-01-27 14:59:30', '2018-01-27 14:59:30');
INSERT INTO `post_options` VALUES ('2', 'Work type 2', '2018-01-27 14:59:30', '2018-01-27 14:59:30');
INSERT INTO `post_options` VALUES ('3', 'Work type 3', '2018-01-27 14:59:30', '2018-01-27 14:59:30');
INSERT INTO `post_options` VALUES ('4', 'Work type 4', '2018-01-27 14:59:30', '2018-01-27 14:59:30');

Consulta:

SELECT *
FROM post_options P
LEFT JOIN post_has_options PHO ON PHO.id_post_work_type = P.id

Agradezco mucho su ayuda.

2 respuestas 2

2

Si no te entendí mal, lo que necesitas es hacer esto:

SELECT P.*,
       PO.*,
       PHO.*
       FROM post P 
       CROSS JOIN post_options PO
       LEFT JOIN post_has_options PHO
          ON PHO.id_post_work_type = PO.id
          AND PHO.Id_post = P.Id
       ORDER BY P.Id, PO.Id;

Mostramos cada post con cuatro filas por post_options mediante un CROSS JOIN o producto cartesiano, por último agregamos la información adicional de post_has_options (si existe) por cada post.id y post_options.id

Resultado:

| id | id_post_type |   title |            create_at |          modified_at | id |        name |            create_at |          modified_at | id_post | id_post_work_type |            create_at |          modified_at |
|----|--------------|---------|----------------------|----------------------|----|-------------|----------------------|----------------------|---------|-------------------|----------------------|----------------------|
|  1 |            1 | Title 1 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  1 | Work type 1 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |       1 |                 1 | 2018-01-27T22:00:51Z | 2018-01-27T22:00:51Z |
|  1 |            1 | Title 1 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  2 | Work type 2 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |       1 |                 2 | 2018-01-27T22:00:54Z | 2018-01-27T22:00:54Z |
|  1 |            1 | Title 1 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  3 | Work type 3 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |       1 |                 3 | 2018-01-27T22:00:58Z | 2018-01-27T22:00:58Z |
|  1 |            1 | Title 1 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  4 | Work type 4 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |  (null) |            (null) |               (null) |               (null) |
|  2 |            1 | Title 2 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  1 | Work type 1 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |  (null) |            (null) |               (null) |               (null) |
|  2 |            1 | Title 2 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  2 | Work type 2 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |       2 |                 2 | 2018-01-27T22:45:19Z | 2018-01-27T22:45:19Z |
|  2 |            1 | Title 2 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  3 | Work type 3 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |  (null) |            (null) |               (null) |               (null) |
|  2 |            1 | Title 2 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  4 | Work type 4 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |  (null) |            (null) |               (null) |               (null) |
|  3 |            1 | Title 3 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  1 | Work type 1 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |  (null) |            (null) |               (null) |               (null) |
|  3 |            1 | Title 3 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  2 | Work type 2 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |  (null) |            (null) |               (null) |               (null) |
|  3 |            1 | Title 3 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  3 | Work type 3 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |  (null) |            (null) |               (null) |               (null) |
|  3 |            1 | Title 3 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  4 | Work type 4 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |  (null) |            (null) |               (null) |               (null) |
|  4 |            1 | Title 4 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  1 | Work type 1 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |  (null) |            (null) |               (null) |               (null) |
|  4 |            1 | Title 4 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  2 | Work type 2 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |  (null) |            (null) |               (null) |               (null) |
|  4 |            1 | Title 4 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  3 | Work type 3 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |  (null) |            (null) |               (null) |               (null) |
|  4 |            1 | Title 4 | 2018-01-27T14:58:24Z | 2018-01-27T23:10:00Z |  4 | Work type 4 | 2018-01-27T14:59:30Z | 2018-01-27T14:59:30Z |  (null) |            (null) |               (null) |               (null) |
1
  • Muchas gracias, ese era el ejemplo. Commented el 28 jun. 2018 a las 18:53
0

Con un left join puedes obtener todos los post que tengan o no tengan options, sin enbargo acá tocaría que usar dos left.

SELECT * FROM post AS p
LEFT JOIN post_has_options AS pho
    ON p.id = pho.id_post
LEFT JOIN post_options AS po
    ON pho.id_post_work_type = po.id

Intenta esa consulta.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.