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

MySQL PHP 表结构

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代码了,先谢过

回复讨论(解决方案)

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

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

这个不就是分组查询么?

SELECT * FROM (SELECT * FROM tORDER BY flag DESC , price DESC)ttGROUP BY mydate

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

select * from tt a where not exists(select 1 from tt

where a.mydate=mydate and a.flag or

a.mydate=mydate and a.flag=flag and a.price )

http://bbs.csdn.net/topics/390476456 这个帖子我拿到了sql版块去问了下。确认了下我那方法好像没问题,我今天也也收获呵呵。

楼上的大神很热心,在我那个帖子里回答了,也在这里回答了。但是我们很少这样用。应该是标准SQL语法

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

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

select * from tt a where not exists(select 1 from tt

where (a.mydate=mydate and a.flag or

a.mydate=mydate and a.flag=flag and a.price and mydate between '2013-05-30 ' and '2013-06-01 '

)

and a.mydate between '2013-05-30 ' and '2013-06-01 '

f68f2add0b68e4f9810432fce46917b7.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值