mysql查询根据时间戳排列来自四个表的数据

时间:2012-11-17 15:20:08

标签: mysql

我试图从四个表中获取关于“类型”的数据。类型可以是注释,覆盖图片更改,朋友请求接受等,但问题是我所做的查询无法通过重复所有类型和所有时间戳。我所做的查询是

SELECT c.up_date, c.user_id, c.id, f.id,
       f.up_date, f.friend1, f.friend2, f.status,
       s.postdate, s.status, s.id, s.display_name, s.userid, s.userid1, s.type,
       c.type, f.type
FROM cover_pic c, wp_userstatus s, wp_friends f
WHERE s.userid = f.friend1
AND s.userid = c.user_id
AND f.status =3
AND c.user_id =4
ORDER BY s.postdate
LIMIT 0 , 30

任何人都可以帮助我处理关于时间戳和类型的正确查询

这是给我的结果

enter image description here

我想要什么

  

timestamp userid_id id display_name type

     

233232323 1 1 paramveer comment

     

1212121212 1 2 paramveer coverpic

1 个答案:

答案 0 :(得分:1)

或许您需要union个加入

(select distinct s.postdate as postdate, s.userid, c.id, c.type
 from wp_userstatus s
 join cover_pic c on s.userid = c.user_id)
union
(select distinct s.postdate, s.userid, f.id, f.type
 from wp_userstatus s
 join wp_friends f on s.userid = f.friend1)
order by postdate
limit 30

这真的很难说。