PHP中的MySQL-如何仅更新表中的一行但具有最大的ID号 [英] Mysql in PHP - how to update only one row in table but with greatest id number

查看:71
本文介绍了PHP中的MySQL-如何仅更新表中的一行但具有最大的ID号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新数据库中的字段,但遇到了一个简单的问题:我只想更新ID号最大的表中的一行.我会做这样的事情:

I am trying to update fields in my DB, but got stuck with such a simple problem: I want to update just one row in the table with the biggest id number. I would do something like that:

UPDATE table SET name='test_name' WHERE id = max(id)

不幸的是,它不起作用.有什么想法吗?

Unfortunatelly it doesnt work. Any ideas?

id | name
---|------
 1 | ghost
 2 | fox
 3 | ghost

我只想更新最后一行,因为ID号是最大的行.

I want to update only last row because ID number is the greatest one.

推荐答案

首先选择最大ID,然后更新.

Select the max id first, then update.

UPDATE table SET name='test_name' WHERE id = (SELECT max(id) FROM table)

这篇关于PHP中的MySQL-如何仅更新表中的一行但具有最大的ID号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆