http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=6420

解决方案 »

  1.   

    运用max 和 group 的不同优先级.
    看看心帆发的faq吧.类似的问题已经处理过很多了。
    先看看,再自己写写才有提高。
    不然直接把结果给你,就算会了也没什么印象.
      

  2.   

    SELECT * from `mytable` where `ID` in('3','4','6','10','11')
      

  3.   

    觉得最近这个问题提的特别多!!!建议犬犬是否将讲述此内容的帖子移到精华帖中?  :-)然后再写个置顶帖,公告一下很多问题在精华区和FAQ区中已有解决方案,给新手或者刚接触CSDN的朋友一个导航!
      

  4.   

    回复人: shuixin13(犬犬(心帆)) 
    请问你给你的贴子为什么会不对?***************************************************************
    我先执行以下语句创建表和新增数据
    CREATE  TABLE  PactRate  (    
    id  int(11)  NOT  NULL  auto_increment,    
    PactId  int(11)  default  NULL,    
    GatheringYear  int(5)  default  NULL,    
    PactCost  int(11)  default  NULL,    
    PRIMARY  KEY  (id),    
    UNIQUE  KEY  id  (id)    
    )  TYPE=MyISAM;    
     
    INSERT  INTO  PactRate  VALUES  (1,  1,  1999,  19990);    
    INSERT  INTO  PactRate  VALUES  (2,  1,  2000,  12000);    
    INSERT  INTO  PactRate  VALUES  (3,  3,  1002,  12000);    
    INSERT  INTO  PactRate  VALUES  (4,  4,  2002,  50000);    
    INSERT  INTO  PactRate  VALUES  (5,  5,  2002,  100); 
    ************************************************************
    然后执行以下语句和结果
    select  *  from  PactRate  group  by  PactId   
    +----+--------+---------------+----------+
    | id | PactId | GatheringYear | PactCost |
    +----+--------+---------------+----------+
    |  1 |      1 |          1999 |    19990 |
    |  3 |      3 |          1002 |    12000 |
    |  4 |      4 |          2002 |    50000 |
    |  5 |      5 |          2002 |      100 |
    +----+--------+---------------+----------+
    *****************************************************
    再执行以下语句和结果
    SELECT    
    SUBSTRING(MAX(CONCAT(gatheringyear,LPAD(id,8,'0'),pactCost)),5,8)+0  AS  id,  
    pactid,  
    LEFT(MAX(CONCAT(gatheringyear,LPAD(id,8,'0'),pactCost)),4)  AS  gatheringyear,  
    SUBSTRING(MAX(CONCAT(gatheringyear,LPAD(id,8,'0'),pactCost)),13)  AS  pactCost    
    FROM  PactRate  GROUP  BY  pactid
    +--------+--------+---------------+----------+
    | id     | pactid | gatheringyear | pactCost |
    +--------+--------+---------------+----------+
    |      2 |      1 | 2000          | 12000    |
    |      3 |      3 | 1002          | 12000    |
    |      4 |      4 | 2002          | 50000    |
    |      5 |      5 | 2002          | 100      |
    +--------+--------+---------------+----------+
    **************************************************************
    问:原 表中的 id=1 和 pactid=1 gatheringyear=1999 pactcost=19990
    怎么会变成了
    id=2 pactid=1 getheringyear=2000 pactcost=12000能否给我一个解释?谢谢!
      

  5.   

    哦!不好意思!我看到了原来的贴子才知道,有两笔记录,就是 pactid=1 有两笔记录,但是在整理的 FAQ 中没有写上
    INSERT INTO PactRate VALUES (2, 1, 2000, 12000);
    所以我没有看到!!