Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql full join (union) and ordering on multiple date columns

Tags:

mysql

A rather complicated sql query that I might be making much more difficult that it should be: I have two tables:

News: newsid, datetime, newstext

Picture: pictureid, datetime, imgPath

The two are not related, I am only joining by the date that the news/picture was created on

SQL so far:

SELECT * FROM news as n LEFT OUTER JOIN (SELECT count(pictureid), datetime 
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), 
datetime FROM picture GROUP BY DATE(datetime)) as p ON 
DATE(n.datetime) = DATE(p.datetime) 

I have to use union to simulate a full outer join in MySQL. The results:

newsid     text     datetime  count()   datetime 
1       sometext   2011-01-16   1       2011-01-16 
2         moo2    2011-01-19  NULL        NULL 
3        mooo3    2011-01-19  NULL        NULL 
NULL      NULL      NULL       4         2011-01-14 

The problem being that I obviously end up with two date columns- one from news and one from pictures, this means i cannot order by date and have it be in the correct order! Any ideas? Even if it means restructuring the database! I need date to be in a single column.

The answer came from SeRPRo The completed working code is:

SELECT `newsid`, `text`,
    CASE 
    WHEN `datetime` IS NULL 
    THEN `pdate` 
    ELSE `datetime` 
    END 
    as `datetime`, 

`pcount` FROM 
(
    (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.pdate) ORDER BY 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.pdate) ORDER BY datetime
    ) 

) as x
ORDER BY datetime
like image 834
Charli Avatar asked Jan 25 '11 10:01

Charli


People also ask

Does full join work in MySQL?

MySQL does not support FULL JOIN, so you have to combine JOIN, UNION and LEFT JOIN to get an equivalent. It gives the results of A union B. It returns all records from both tables.

Is full outer join faster than UNION?

Union will be faster, as it simply passes the first SELECT statement, and then parses the second SELECT statement and adds the results to the end of the output table.

Can we use full outer join in MySQL?

MySQL doesn't offer syntax for a full outer join, but you can implement one using the union of a left and a right join.


1 Answers

just using your database structure and your query, and since FULL OUTER JOIN is not available in MySQL, I think a solution could be this:

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) 

)
like image 103
SERPRO Avatar answered Nov 03 '22 00:11

SERPRO