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

Unión completa de MySql (unión) y pedido en múltiples columnas de fecha

simplemente usando la estructura de su base de datos y su consulta, y dado que FULL OUTER JOIN no está disponible en MySQL, creo que una solución podría ser esta:

SELECT
    `newsid`,
    `text`,
    CASE
        WHEN `datetime` IS NULL THEN `pdate`
        ELSE `datetime`
    END as `datetime,
    `pcount`
(
SELECT *
FROM `news` as `n`
    LEFT OUTER JOIN (
                        SELECT count(pictureid) as `pcount`, datetime as `pdate`
                        FROM picture GROUP BY DATE(datetime)
                    ) as p ON DATE(n.datetime) = DATE(p.datetime) 
    UNION
SELECT * 
    FROM `news` as `n`
    RIGHT OUTER JOIN (
                        SELECT count(pictureid) as `pcount`, datetime as `pdate`
                        FROM picture GROUP BY DATE(datetime)
                    ) as p ON DATE(n.datetime) = DATE(p.datetime) 

)