Home > Database > Mysql Tutorial > MySQL 更新并返回计数_MySQL

MySQL 更新并返回计数_MySQL

WBOY
Release: 2016-06-01 13:06:36
Original
1052 people have browsed it

对于想使用 MySQL 实现简单 ID 分配器, 一般就是设置一个整数字段, 然后想分配的时候加 1 并返回. 新手往往犯错误, 先执行一条 update 语句, 然后再 select 那个字段. 但这是错误的!

正确的方式应该是使用 last_insert_id() 函数:

update mytable set count=last_insert_id(counter+1);select last_insert_id();
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template