表结构
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
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
FROM (SELECT *
FROM t
ORDER BY flag DESC , price DESC
)tt
GROUP BY mydate
where a.mydate=mydate and a.flag<flag
or
a.mydate=mydate and a.flag=flag and a.price<price
)
楼上的大神很热心,在我那个帖子里回答了,也在这里回答了。但是我们很少这样用。应该是标准SQL语法
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 '