SQL基于Timestamp差异计算ON Multi table with multi condition - MySQL

时间:2011-04-16 04:37:26

标签: mysql sql timestamp

表格结构和示例数据

Wall_Update

   [INT         VARCHAR       VARCHAR         TIMESTAMP               TinyText]   
   pid [pk] |  CreatedBy  |   RepliedBy   |     Time               |     Text    

   1        |   sourav    |     sachin    |   2011-04-09 21:26:01  |      Hi

  UID  |    fUID

sourav |   sachin  
sachin |   sourav

我需要获得由SOURAV或SOURAV的朋友创建的所有帖子的PID,而且时间应该是3天之久!

所以我尝试了这两个查询[并且都失败了]

select pid,`Text`,CreatedBy,Time,RepliedBy from WALL_UPDATE where CreatedBy in 
  (select FUID from friends where UID='sourav') or CreatedBy='sourav' and 
    TIMESTAMPDIFF (DAY , Time,CURRENT_TIMESTAMP)<=3 order by PID desc

SELECT WU.pid,WU.Text,MN.NameF,WU.Time,WU.CreatedBy FROM wall_update WU,main MN WHERE
  WU.CreatedBy in (select FUID from FRIENDS where UID='sourav') or
  WU.CreatedBy='sourav' and MN.uid=WU.createdBy and 
     TIMESTAMPDIFF (DAY , WU.time,CURRENT_TIMESTAMP ) <3 group by WU.pid

[都返回由SOURAV或SOURAV的朋友创建的所有PID]

1 个答案:

答案 0 :(得分:1)

试试这个

  

从WALL_UPDATE中选择*   (CreatedBy IN(选择fuid来自   朋友,其中uid ='sourav')           或CreatedBy ='sourav')           和TIMESTAMPDIFF(DAY,Time,now())&lt; = 3
          通过pid desc订购

使用主要ID来引用朋友用户而不是名称,这将创建dublicate