SELECT a.LINKMAN_ID,b.CUSTOMER_ID,LINKMAN_NAME,SEX,a.TEL_NO_WORK,a.MOBILE,a.EMAIL as EMAIL,a.OICQ,a.ICQ,b.CUSTOMER_NAME,c.CONTACT_DATE,c.SELLER FROM `LINKMAN` a LEFT OUTER JOIN CUSTOMER b ON a.`CUSTOMER_ID` = b.`CUSTOMER_ID` LEFT OUTER JOIN CONTACT c on c.`CUSTOMER_ID`=b.`CUSTOMER_ID` where 1=1 ORDER BY a.CUSTOMER_ID desc LIMIT 0 ,10CONTACT表中有一个字段[contact_date]为时间类型.结构如下:contact_id  customer_id contact_seller contact_date
1            122           张          2008-8-20
2            153           李        2008-8-21
3            122           张        2008-8-25
4            239           王        2008-8-26
 
我想查询最近时间联系的一条记录,请帮忙改一下上面的语句

解决方案 »

  1.   

    加上order by 倒序,取一条就行了。
      

  2.   

    谢谢:yanxiao_jiang 我要的结果是:
    LINKMAN_ID LINKMAN_NAME contact_id  customer_id contact_seller contact_date 
      X       X       2            153          李       2008-8-21 
      X       X       3            122          张       2008-8-25 
      X       X       4            239          王       2008-8-26 不是只要一条
      

  3.   

    LEFT OUTER JOIN CONTACT c
     这个地方
    改称
    LEFT OUTER JOIN ( select CUSTOMER_ID,max(contact_date) from CONTACT  group by CONTACT_date) c
    看看是否是你需要的。
      

  4.   

    m个类型取前n条的问题
    如果n=1
         一条sql语句可以完成
    如果n>1的话
         一条语句是不能完成的,需要程序完成
    __________________________________________
    你的要求可用select contact_id,customer_id,contact_seller,max(contact_date)
     from contact
     group by customer_id;来完成
      

  5.   

    select a.* from tt a inner join
    (select customer_id,max(contact_date) as ma from tt group by customer_id) b
    on a.customer_id=b.customer_id and a.contact_date=b.ma
      

  6.   

    谢谢大家了.来者有分.我的mysql是4.0的,用不了子查询.所以上述都不对.结贴给分: