现在有两个表
table1字段:id,uid,pid,time(分别为自动编号,用户id,产品id,时间戳)记录   1,3,2,1295271132
2,3,1,1295271132
3, 5, 1, 1102565336.....table2字段:id,uid,title,time(分别为自动编号,用户id,标题,时间戳)记录   1,3,第一标题,1295271132
2,3,第二个标题,1295271132
3, 5, 第三个标题, 1102565336.....
他们两个表之间是没有关联的,只有其中的uid是一致的执行插入操作时,是分别插入两个表的现在的需要,我要在一个页面中 把两个表的数据同时按照时间(就是以天为单位)列出来并且各自的表中,凡是时间戳一致且uid也一致,就只显示一条也就是说上边的数据,执行出来的结果应该一共只显示4条记录,每个表的第一和第二条记录是时间戳和uid一致的,只算一条求思路

解决方案 »

  1.   

    select a.* from t1 a where not exists(select 1 from t1 where a.uid=uid and a.time<time)
    union 
    select a.* from t2 a where not exists(select 1 from t2 where a.uid=uid and a.time<time)
      

  2.   


    感谢WWWWA我在phpmyadmin中执行,报错如下:#1222 - The used SELECT statements have a different number of columns t1 和 t2  的列数是不同的不好意思,我没有说明白,t1和t2的字段数量不是同的,后边各自还跟有其他的字段
      

  3.   

    那就加上具体字段名
    如id,uid,pid,time
    select id,uid,pid,time from t1 a where not exists(select 1 from t1 where a.uid=uid and a.time<time)
    union 
    select id,uid,pid,time from t2 a where not exists(select 1 from t2 where a.uid=uid and a.time<time)