R
R
rgen32012-10-16 09:13:20
MySQL
rgen3, 2012-10-16 09:13:20

Is it possible to combine INSERT and SELECT in one question?

Good morning, Khabravchane!
Can you please tell me if it is possible to combine INSERT and SELECT in one query?
The task is as follows:
There is a label `texts`
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`text` longtext NOT NULL,
`link` bigint(20) NOT NULL,
`date` text NOT NULL,
`time` text NOT NULL, It is
necessary to insert values ​​into the table, and then immediately pull out the id of the last added record,

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
vsespb, 2012-10-16
@vsespb

LAST_INSERT_ID()

P
pletinsky, 2012-10-16
@pletinsky

Maybe the OUTPUT syntax will work for you.
For MySQL , LAST_INSERT_ID();
For MSSQL SCOPE_IDENTITY();

D
Denis Turenko, 2012-10-16
@Dennion

You can use the construct SET @lastID := LAST_INSERT_ID();and then use @lastID in the right place. At least that's how my price list processor works.
INSERT INTO `my_table` SET uid="Х-1234", pic_small="/UserFiles/Image/Х-1234_1s.jpg", pic_big="/UserFiles/Image/Х-1234_1.jpg", name="BOSCH", price="1200";
SET @lastID := LAST_INSERT_ID(); INSERT INTO `my_table_foto` SET [email protected], name="/UserFiles/Image/Х-1234_1.jpg";

E
EllaVS, 2012-10-16
@EllaVS

I use SCOPE_IDENTITY() as pletinsky said. Those. INSERT goes first, then SELECT separated by a semicolon:
string strSQL = "INSERT INTO ...."; strSQL = strSQL + "; SELECT SCOPE_IDENTITY() AS NewID;";

S
Stepan, 2012-10-16
@L3n1n

On php you can use

if(mysql_query("INSERT ...")) $last_id=mysql_insert_id();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question