SELECT插入行的索引?

时间:2014-04-19 05:38:16

标签: mysql sql select

我需要做2个mysql查询:

1st- That insert some data into mi database with an autoincreasing Index (managed by mysql)
2nd- I need to use this index into another SELECT query.

现在我正在做:

- INSERT INTO .....
- SELECT Index FROM.... (Index of the row just added)
- SELECT * FROM ... WHERE ID=Index

还有更多的permormancing解决方案吗?

由于

2 个答案:

答案 0 :(得分:2)

有一个函数可以知道当前连接中插入的最后一个id

mysql_query('INSERT INTO FOO(a) VALUES(\'b\')');
$id = mysql_insert_id();

你的SQL查询现在

SELECT * FROM ... WHERE ID=$id

该功能称为mysql_insert_id

答案 1 :(得分:0)

Try below one.

INSERT INTO .....
SELECT * FROM ... WHERE ID = LAST_INSERT_ID();