php+mysql 取字段值比较 相同则比较另一字段值

potter2002 2013-05-30 10:07:24
表结构
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代码了,先谢过
...全文
346 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
WWWWA 2013-06-03
  • 打赏
  • 举报
回复
引用 8 楼 potter2002 的回复:
to WWWWA:指定日期期间这个条件怎么加上去?
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 '
potter2002 2013-06-02
  • 打赏
  • 举报
回复
to WWWWA:指定日期期间这个条件怎么加上去?
WWWWA 2013-05-31
  • 打赏
  • 举报
回复
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 )
梦康 2013-05-31
  • 打赏
  • 举报
回复
楼主是不是先比较flag大小,如果flag大小相同则再比较price大小?试试4楼的方法(其实这个问题我也不会,是完全看得2楼的;我本来想group by xx order by xxx,发现使用了group by 是直接取第一个,后面的order by 好像失效了。也才明白为什么二楼那样写了)
梦康 2013-05-31
  • 打赏
  • 举报
回复
SELECT * 
FROM (

SELECT * 
FROM t
ORDER BY flag DESC , price DESC
)tt
GROUP BY mydate
  • 打赏
  • 举报
回复
这个不就是分组查询么?
xuzuning 2013-05-31
  • 打赏
  • 举报
回复
select id, mydate, max(price) as price, flag from (select * from tbl_name order by price desc) t group by mydate
梦康 2013-05-31
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/390476456 这个帖子我拿到了sql版块去问了下。确认了下我那方法好像没问题,我今天也也收获呵呵。 楼上的大神很热心,在我那个帖子里回答了,也在这里回答了。但是我们很少这样用。应该是标准SQL语法
一起混吧 2013-05-30
  • 打赏
  • 举报
回复
select * from (select * from tt order by flag desc) t group by mydate;

21,882

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧