期望:在mysql中
变通实现类似oracle中sequence的功能,自增,而且可以通过sql语句获得,
使用场景:将取得的值插入到两张表中,使这两张表的主键相同,问题就是如何取得这个值?希望高手指点,如何实现。
变通实现类似oracle中sequence的功能,自增,而且可以通过sql语句获得,
使用场景:将取得的值插入到两张表中,使这两张表的主键相同,问题就是如何取得这个值?希望高手指点,如何实现。
解决方案 »
- 关于利用MYSQL数据库自动导出数据至EXCEL问题
- 我的mysql自动关闭,原因不明,错误日志终于拿到了,但是看不懂,谁能看懂,帮帮忙哈。
- 新手,刚接触mysql,问一个数据表里数据复制到另个表的问题!
- mysql按日统计
- 【求助】应用从ORACLE转MYSQL数据库的疑问
- postgresql 磁盘备份
- 急求一个没有用户名和密码的连接字符串如何写?(getConnection("????"))
- LIMIT 不停止扫描?
- 如何修改mysql的最大连接数阿,我用的是4.1.7版本 ?
- 高手请进
- mysql 超难的查询随机问题
- 存储过程不存在?!(PROCEDURE does not exist)
去看看吧!这里讨论得很详细。
Query OK, 0 rows affected (0.13 sec)mysql> insert into t_csdn1111(v) values ('A1');
Query OK, 1 row affected (0.06 sec)mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
mysql> select * from t_csdn1111 where id is null;
+----+------+
| id | v |
+----+------+
| 1 | A1 |
+----+------+
1 row in set (0.00 sec)mysql> select auto_increment from information_schema.tables where table_schema='
csdn' and table_name='t_csdn1111';
+----------------+
| auto_increment |
+----------------+
| 2 |
+----------------+
1 row in set (0.08 sec)mysql>
mysql> insert into t_csdn1111(v) values ('A21'),('A22'),('A23');
Query OK, 3 rows affected (0.06 sec)
Records: 3 Duplicates: 0 Warnings: 0mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 2 |
+------------------+
1 row in set (0.00 sec)mysql> select * from t_csdn1111 where id is null;
+----+------+
| id | v |
+----+------+
| 2 | A21 |
+----+------+
1 row in set (0.00 sec)mysql> select auto_increment from information_schema.tables where table_schema='
csdn' and table_name='t_csdn1111';
+----------------+
| auto_increment |
+----------------+
| 5 |
+----------------+
1 row in set (0.00 sec)mysql>
[code=SQL]select last_insert_id()注意:这个只适合单个insert的情况,不适合批量的情况,如 insrt into tb_name values(1),(2),(3)...若要获取全局的自增id:
select auto_increment from information_schema.tables where table_schema='
库名' and table_name='表名';[/code]
select last_insert_id()注意:这个只适合单个insert的情况,不适合批量的情况,如 insrt into tb_name values(1),(2),(3)... 若要获取全局的自增id:
select auto_increment from information_schema.tables where table_schema='
库名' and table_name='表名';