表结构
id             int 主健自增
mydate        datetime
price         int
flag          int  数值为1,2,3,4,5,6  优先取该值大的price
示例
id      mydate          price   flag
1       2013-05-30      200      2
2       2013-05-30      300      3
3       2013-05-31      150      2
4       2013-05-31      200      3
5       2013-06-01      200      2
6       2013-06-01      300      3结果为
id      mydate          price   flag
1       2013-05-30      300      3
2       2013-05-31      200      3
3       2013-06-01      350      3
php代码如何写?很久没写过php代码了,先谢过MySQLPHP

解决方案 »

  1.   

    select * from (select * from tt order by flag desc) t group by mydate;
      

  2.   

    select id, mydate, max(price) as price, flag from (select * from tbl_name order by price desc) t group by mydate
      

  3.   

    SELECT * 
    FROM (SELECT * 
    FROM t
    ORDER BY flag DESC , price DESC
    )tt
    GROUP BY mydate
      

  4.   

    楼主是不是先比较flag大小,如果flag大小相同则再比较price大小?试试4楼的方法(其实这个问题我也不会,是完全看得2楼的;我本来想group by xx order by xxx,发现使用了group by 是直接取第一个,后面的order by 好像失效了。也才明白为什么二楼那样写了)
      

  5.   

    select * from tt a where not exists(select 1 from tt
     where a.mydate=mydate and a.flag<flag
     or
     a.mydate=mydate and a.flag=flag and a.price<price
     )
      

  6.   

    http://bbs.csdn.net/topics/390476456 这个帖子我拿到了sql版块去问了下。确认了下我那方法好像没问题,我今天也也收获呵呵。
    楼上的大神很热心,在我那个帖子里回答了,也在这里回答了。但是我们很少这样用。应该是标准SQL语法
      

  7.   

    to WWWWA:指定日期期间这个条件怎么加上去?
      

  8.   


    select * from tt a where not exists(select 1 from tt
      where (a.mydate=mydate and a.flag<flag
      or
      a.mydate=mydate and a.flag=flag and a.price<price)
      and mydate between '2013-05-30 ' and '2013-06-01 '
      )
    and a.mydate between '2013-05-30 ' and '2013-06-01 '