Chinaunix首页 | 论坛 | 博客
  • 博客访问: 535101
  • 博文数量: 63
  • 博客积分: 1194
  • 博客等级: 中士
  • 技术积分: 761
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 12:44
个人简介

得之坦然,失之淡然,争其必然,顺其自然!

文章分类

全部博文(63)

文章存档

2014年(2)

2013年(22)

2012年(39)

分类: Mysql/postgreSQL

2013-11-08 10:21:18

会话1:

点击(此处)折叠或打开

  1. mysql> create table test (a int);
  2. Query OK, 0 rows affected (0.18 sec)

  3. mysql> insert into test values(1);
  4. Query OK, 1 row affected (0.00 sec)

  5. mysql> select * from test;
  6. +------+
  7. | a |
  8. +------+
  9. | 1 |
  10. +------+
  11. 1 row in set (0.00 sec)

  12. mysql>
会话2:

点击(此处)折叠或打开

  1. mysql> select * from test;
  2. +------+
  3. | a |
  4. +------+
  5. | 1 |
  6. +------+
  7. 1 row in set (0.00 sec)

  8. mysql> update test set a=LAST_INSERT_ID(a+1);
  9. Query OK, 1 row affected (0.00 sec)
  10. Rows matched: 1 Changed: 1 Warnings: 0

  11. mysql> select LAST_INSERT_ID();
  12. +------------------+
  13. | LAST_INSERT_ID() |
  14. +------------------+
  15. | 2 |
  16. +------------------+
  17. 1 row in set (0.00 sec)

  18. mysql> select * from test;
  19. +------+
  20. | a |
  21. +------+
  22. | 2 |
  23. +------+
  24. 1 row in set (0.00 sec)

  25. mysql>
会话1:

点击(此处)折叠或打开

  1. mysql> update test set a=LAST_INSERT_ID(a+1);
  2. Query OK, 1 row affected (0.02 sec)
  3. Rows matched: 1 Changed: 1 Warnings: 0

  4. mysql> select LAST_INSERT_ID();
  5. +------------------+
  6. | LAST_INSERT_ID() |
  7. +------------------+
  8. | 3 |
  9. +------------------+
  10. 1 row in set (0.00 sec)

  11. mysql> select * from test;
  12. +------+
  13. | a |
  14. +------+
  15. | 3 |
  16. +------+
  17. 1 row in set (0.00 sec)

  18. mysql>
会话2:

点击(此处)折叠或打开

  1. mysql> select LAST_INSERT_ID();
  2. +------------------+
  3. | LAST_INSERT_ID() |
  4. +------------------+
  5. | 2 |
  6. +------------------+
  7. 1 row in set (0.00 sec)

  8. mysql> select * from test;
  9. +------+
  10. | a |
  11. +------+
  12. | 3 |
  13. +------+
  14. 1 row in set (0.00 sec)

  15. mysql>
由此可见,在使用LAST_INSERT_ID()函数更新字段值后,使用select LAST_INSERT_ID(),能够获取当前会话最后一次更新的数值,而不受其它会话更新的影响
阅读(7250) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~