mysql 取最大值列的问题

需求: 查询/更新某列(id) 最大值的行.

尝试1: 

select * from table where id =max(select max(id) from table); --可以通过

update table set name='new name' where  id =max(select max(id) from table); --不能可以通过

不能通过原因 ,求解释?

尝试2: 通过用户变量:(可行)

select    @maxid  :=max(id) from hchkpage;

update hchkpage set payeename  = 'new name' where id =@maxid;

此方法可行, 局限在数据库客户端, 在程序中不能使用.

通过参考mysql官方文档:

http://dev.mysql.com/doc/refman/5.1/zh/index.html

http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#update

想到此方法:

update hchkpage set payeename='good' order by id desc limit 1;

:通过条件排序和使用limit限制查询条数可取得最大行 ,执行更新.

总结: 遇到问题参考官方文档.

猜你喜欢

转载自qdpurple.iteye.com/blog/1700497