<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[mysql的last_insert_id()用法]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Tue, 21 Oct 2008 10:50:58 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	最近在做一个小东西的时候，发现mysql有last_insert_id()这个函数<br/><br/>它的用法如下：记录下来备忘<br/><br/>它必需紧跟在insert 语句执行之后。<br/><br/>//执行insert语句先<br/><br/>$sql=&quot;insert into table (name1,name2,...) values(&#039;value1&#039;,&#039;values&#039;...)&quot;;<br/><br/> dbx_query($db_link,$sql);<br/><br/>//找出最后一次插入记录的id<br/><br/>$select=&quot;select last_insert_id() &quot;;<br/><br/> $result=dbx_query($db_link,$select);<br/><br/>$last_id=$result-&gt;data[0][0];<br/><br/><br/>ps: $last_id=mysql_insert_id();<br/><br/>mysql_insert_id() 将 MySQL 内部的 C API 函数 mysql_insert_id() 的返回值转换成 long（PHP 中命名为 int）。如果 AUTO_INCREMENT 的列的类型是 BIGINT，则 mysql_insert_id() 返回的值将不正确。可以在 SQL 查询中用 MySQL 内部的 SQL 函数 LAST_INSERT_ID() 来替代。 <br/><br/>具体的请看php手册: mysql_insert_id<br/><br/>来自：http://blog.csdn.net/kemy88/article/details/1108524<br/><br/><br/><br/><br/><br/><br/><br/><br/>_____________________________________________________________________________<br/>show table status&#92;G<br/>show create table message;<br/>LAST_INSERT_ID<br/><br/>自动返回最后一个 INSERT 或 UPDATE 操作为 AUTO_INCREMENT 列设置的第一个发生的值. 参考这里 <br/><br/>The ID that was generated is maintained in the server on a per-connection basis.<br/><br/>LAST_INSERT_ID是基于单个connection的, 不可能被其它的客户端连接改变。<br/><br/> 可以用 SELECT LAST_INSERT_ID(); 查询LAST_INSERT_ID的值.<br/><br/>Important: If you insert multiple rows using a single INSERT statement, LAST_INSERT_ID() returns the value generated for the first inserted row only.<br/><br/>使用单INSERT语句插入多条记录,&nbsp;&nbsp;LAST_INSERT_ID只返回插入的第一条记录产生的值. 比如<br/><br/><br/><div class="code">&nbsp;&nbsp; 1. mysql&gt; INSERT INTO t VALUES (NULL, &#039;aaaa&#039;), (NULL, &#039;bbbb&#039;), (NULL, &#039;cccc&#039;);&nbsp;&nbsp; <br/>&nbsp;&nbsp; 2. mysql&gt; SELECT * FROM t;&nbsp;&nbsp; <br/>&nbsp;&nbsp; 3. +----+------+&nbsp;&nbsp; <br/>&nbsp;&nbsp; 4. &#124; id &#124; name &#124;&nbsp;&nbsp; <br/>&nbsp;&nbsp; 5. +----+------+&nbsp;&nbsp; <br/>&nbsp;&nbsp; 6. &#124;&nbsp;&nbsp;1 &#124; Bob&nbsp;&nbsp;&#124;&nbsp;&nbsp; <br/>&nbsp;&nbsp; 7. &#124;&nbsp;&nbsp;2 &#124; aaaa &#124;&nbsp;&nbsp; <br/>&nbsp;&nbsp; 8. &#124;&nbsp;&nbsp;3 &#124; bbbb &#124;&nbsp;&nbsp; <br/>&nbsp;&nbsp; 9. &#124;&nbsp;&nbsp;4 &#124; cccc &#124;&nbsp;&nbsp; <br/>&nbsp;&nbsp;10. +----+------+&nbsp;&nbsp; <br/>&nbsp;&nbsp;11. mysql&gt; SELECT LAST_INSERT_ID();&nbsp;&nbsp; <br/>&nbsp;&nbsp;12. +------------------+&nbsp;&nbsp; <br/>&nbsp;&nbsp;13. &#124; LAST_INSERT_ID() &#124;&nbsp;&nbsp; <br/>&nbsp;&nbsp;14. +------------------+&nbsp;&nbsp; <br/>&nbsp;&nbsp;15. &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 &#124;&nbsp;&nbsp; <br/>&nbsp;&nbsp;16. +------------------+&nbsp;&nbsp;</div><br/><br/>ID 2 是在插入第一条记录aaaa 时产生的.<br/><br/>LAST_INSERT_ID 是与table无关的，如果向表a插入数据后，再向表b插入数据，LAST_INSERT_ID会改变。<br/><br/>一般情况下获取刚插入的数据的id，使用select max(id) from table 是可以的。<br/><br/>但在多线程情况下，就不行了。在多用户交替插入数据的情况下max(id)显然不能用。<br/><br/>这就该使用LAST_INSERT_ID了，因为LAST_INSERT_ID是基于Connection的，只要每个线程都使用独立的Connection对象，LAST_INSERT_ID函数将返回该Connection对AUTO_INCREMENT列最新的insert or update操作生成的第一个record的ID。这个值不能被其它客户端（Connection）影响，保证了你能够找回自己的 ID 而不用担心其它客户端的活动，而且不需要加锁。<br/><br/>mysql的last_insert_id()不可靠 Mysql的API有个很有意义的函数last_insert_id()。这个函数的作用是，针对auto_increment字段，返回给定的 数据库链接，上一步 INSERT 查询中产生的 AUTO_INCREMENT 的 ID 号。如果没有指定数据库链接，则使用上一个打开的连接。&nbsp;&nbsp; 很多人质疑last_insert_id()是否是可靠的,以前我也犹豫过。&nbsp;&nbsp; 事实上的结果是mysql_insert_id决不会取错。首先做个测试，在mysql_query(&quot;insert.....);之后立刻sleep（1100），其间再做些其他的insert. 然后发现在mysql_insert_id取的值都不会和其他的冲突。&nbsp;&nbsp; 看了半天mysql的代码。mysql_insert_id是这么定义的<br/><br/>my_ulonglong STDCALL mysql_insert_id(MYSQL *mysql) &#123;&nbsp;&nbsp; return mysql-&gt;;last_used_con-&gt;;insert_id; &#125;<br/><br/><br/>参考：http://zhaohe162.blog.163.com/blog/static/38216797201122411193745/
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] mysql的last_insert_id()用法]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>