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


a rather complicated sql query might making more difficult should be: have 2 tables:

news: newsid, datetime, newstext

picture: pictureid, datetime, imgpath

the 2 not related, joining date news/picture created on

sql far:

select * news n left outer join (select count(pictureid), datetime  picture group date(datetime)) p on date(n.datetime) = date(p.datetime)  union  select * news n right outer join (select count(pictureid),  datetime picture group date(datetime)) p on  date(n.datetime) = date(p.datetime)  

i have use union simulate full outer join in mysql. 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 end 2 date columns- 1 news , 1 pictures, means cannot order date , have in correct order! ideas? if means restructuring database! need date in single column.

the answer came serpro completed working code is:

select `newsid`, `text`,     case      when `datetime` null      `pdate`      else `datetime`      end      `datetime`,   `pcount`  (     (select * news n left outer join          (select count(pictureid) pcount, datetime pdate picture group date(datetime)) p          on date(n.datetime) = date(p.pdate) order datetime     )     union     (select * news n right outer join          (select count(pictureid) pcount, datetime pdate picture group date(datetime)) p          on date(n.datetime) = date(p.pdate) order datetime     )   ) x order datetime 

just using database structure , query, , since full outer join not available in mysql, think solution this:

select     `newsid`,     `text`,     case         when `datetime` null `pdate`         else `datetime`     end `datetime,     `pcount` ( select * `news` `n`     left outer join (                         select count(pictureid) `pcount`, datetime `pdate`                         picture group date(datetime)                     ) p on date(n.datetime) = date(p.datetime)      union select *      `news` `n`     right outer join (                         select count(pictureid) `pcount`, datetime `pdate`                         picture group date(datetime)                     ) p on date(n.datetime) = date(p.datetime)   ) 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

sql server - python to mssql encoding problem -

windows - Python Service Installation - "Could not find PythonClass entry" -