sql >> Base de Datos >  >> RDS >> Mysql

¿Cómo emular la unión externa completa en esta consulta?

Básicamente haces una Unión de IZQUIERDA y DERECHA.

De hecho, tiene una arruga interesante en el sentido de que también desea limitar las filas a 3. Para resolver eso, necesita

  • Limite las selecciones "izquierda" y "derecha" a 3
  • Luego use el resultado de UNION en una vista en línea
  • luego Limite la unión por 3 de nuevo

ACTUALIZAR Lamentablemente, a menos que me equivoque, no puede hacer esto directamente en UNION, por lo que debe agregar otra capa de vistas en línea antes de UNION

Los LÍMITES dentro de UNION ofrecerán algún beneficio de rendimiento y luego el límite posterior le dará los resultados correctos.

SELECT title, 
   teaser, 
   nid, 
   DATE, 
   image, 
   image_tid 
FROM   (SELECT title, 
               teaser, 
               nid, 
               DATE, 
               image, 
               image_tid,
               created 
        FROM   (SELECT DISTINCT n.title, 
                                nr.teaser, 
                                n.nid, 
                                Date_format(From_unixtime(n.created), 
                                '%M %e, %Y') AS 
                                DATE, 
                                f.filepath 
                                AS 
                                image, 
                                tn_img.tid 
                                AS 
                                image_tid 
                                       , 
                                n.created 
                FROM   node n 
                       JOIN node_revisions nr 
                         ON n.nid = nr.nid 
                       LEFT JOIN content_field_related_images cfri 
                         ON ( n.nid = cfri.nid 
                              AND cfri.delta = 0 ) 
                       LEFT JOIN content_field_att_file cfaf 
                         ON cfri.field_related_images_nid = cfaf.nid 
                       LEFT JOIN files f 
                         ON cfaf.field_att_file_fid = f.fid 
                       JOIN term_node tn2 
                         ON n.nid = tn2.nid 
                       LEFT OUTER JOIN term_node tn_img 
                         ON cfri.field_related_images_nid = tn_img.nid 
                WHERE  n.status = 1 
                       AND n.TYPE = 'article' 
                       AND nr.body LIKE '%kimberly-clark%' 
                       AND tn2.tid = 143 
                ORDER  BY n.created DESC 
                LIMIT  3) tleft 
        UNION 
        SELECT title, 
               teaser, 
               nid, 
               DATE, 
               image, 
               image_tid,
               created  
        FROM   (SELECT DISTINCT n.title, 
                                nr.teaser, 
                                n.nid, 
                                Date_format(From_unixtime(n.created), 
                                '%M %e, %Y') AS 
                                DATE, 
                                f.filepath 
                                AS 
                                image, 
                                tn_img.tid 
                                AS 
                                image_tid 
                                       , 
                                n.created 
                FROM   node n 
                       JOIN node_revisions nr 
                         ON n.nid = nr.nid 
                       LEFT JOIN content_field_related_images cfri 
                         ON ( n.nid = cfri.nid 
                              AND cfri.delta = 0 ) 
                       LEFT JOIN content_field_att_file cfaf 
                         ON cfri.field_related_images_nid = cfaf.nid 
                       LEFT JOIN files f 
                         ON cfaf.field_att_file_fid = f.fid 
                       JOIN term_node tn2 
                         ON n.nid = tn2.nid 
                       RIGHT OUTER JOIN term_node tn_img 
                         ON cfri.field_related_images_nid = tn_img.nid 
                WHERE  n.status = 1 
                       AND n.TYPE = 'article' 
                       AND nr.body LIKE '%kimberly-clark%' 
                       AND tn2.tid = 143 
                ORDER  BY n.created DESC 
                LIMIT  3) tright) t 
ORDER  BY created DESC 
LIMIT  3 

ACTUALIZAR Usando las sugerencias de spencer7593 y ypercube, aquí hay un enfoque alternativo que usa dos instrucciones UNION ALL y ninguna vista en línea.

SELECT DISTINCT n.created, 
                n.title, 
                nr.teaser, 
                n.nid, 
                Date_format(From_unixtime(n.created), '%M %e, %Y') AS DATE, 
                f.filepath                                         AS image, 
                tn_img.tid                                         AS image_tid 
FROM   node n 
       JOIN node_revisions nr 
         ON n.nid = nr.nid 
       LEFT JOIN content_field_related_images cfri 
         ON ( n.nid = cfri.nid 
              AND cfri.delta = 0 ) 
       LEFT JOIN content_field_att_file cfaf 
         ON cfri.field_related_images_nid = cfaf.nid 
       LEFT JOIN files f 
         ON cfaf.field_att_file_fid = f.fid 
       JOIN term_node tn2 
         ON n.nid = tn2.nid 
       LEFT OUTER JOIN term_node tn_img 
         ON cfri.field_related_images_nid = tn_img.nid 
WHERE  n.status = 1 
       AND n.TYPE = 'article' 
       AND nr.body LIKE '%kimberly-clark%' 
       AND tn2.tid = 143 

UNION ALL 
SELECT DISTINCT n.created, 
                n.title, 
                nr.teaser, 
                n.nid, 
                Date_format(From_unixtime(n.created), '%M %e, %Y') AS DATE, 
                f.filepath                                         AS image, 
                tn_img.tid                                         AS image_tid 
FROM   node n 
       JOIN node_revisions nr 
         ON n.nid = nr.nid 
       LEFT JOIN content_field_related_images cfri 
         ON ( n.nid = cfri.nid 
              AND cfri.delta = 0 ) 
       LEFT JOIN content_field_att_file cfaf 
         ON cfri.field_related_images_nid = cfaf.nid 
       LEFT JOIN files f 
         ON cfaf.field_att_file_fid = f.fid 
       JOIN term_node tn2 
         ON n.nid = tn2.nid 
       RIGHT JOIN term_node tn_img 
         ON cfri.field_related_images_nid = tn_img.nid 
WHERE  n.status = 1 
       AND n.TYPE = 'article' 
       AND nr.body LIKE '%kimberly-clark%' 
       AND tn2.tid = 143 
       AND cfri.field_related_images_nid IS NULL 
ORDER  BY 1 DESC 
LIMIT 
3