Mysql
 sql >> Database >  >> RDS >> Mysql

MySql full join (unione) e ordinamento su più colonne di data

semplicemente usando la struttura del tuo database e la tua query, e poiché FULL OUTER JOIN non è disponibile in MySQL, penso che una soluzione potrebbe essere questa:

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) 

)