union all关联两个不相关的表,按时间排序

表uc_diary与uc_photo是两个完全不相关的表,用union all可以将这里个表的数据关联到一起,使用时间排序,常用于新鲜事的显示,讲日志,相册,说说等动态放到一起,安装时间排序

select diary_id as id,
       diary_title as title,
       diary_text as contentandname,
       post_time,
       'diary' as type 
from 
uc_diary 
where
diary_state='0' AND emp_id='eedc8ae7-be7b-41d6-8887-283804d6aac9'
union all
select photo_id as id,
       (select title from uc_album where album_id=uc.album_id AND album_state='0' and emp_id='eedc8ae7-be7b-41d6-8887-283804d6aac9') as title,
       filename as contentandname,
       post_time,
       'photo' as type 
from 
uc_photo uc
where photo_state='0' and emp_id='eedc8ae7-be7b-41d6-8887-283804d6aac9'
order by post_time desc

 

posted @ 2013-12-04 17:07  X.Y  阅读(510)  评论(0编辑  收藏  举报