D
D
DVoropaev2019-03-30 00:36:09
MySQL
DVoropaev, 2019-03-30 00:36:09

How to insert the last ID from another table into a field?

I do this INSERT:

INSERT INTO galaxy (name, type, diametr FLOAT, mass, discovery_date, birth_date FLOAT, astronomical_object ) VALUES (...)

in the field "astronomical_object" you need to put the latest ID from the table of the same name

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2019-03-30
@DVoropaev

INSERT INTO galaxy (name, type, diametr FLOAT, mass, discovery_date, birth_date FLOAT, 
  astronomical_object ) 
VALUES (..., (SELECT MAX('id') FROM astronomical_object))

Последний ID, если вы его не сохраняете триггером, вы не сможете найти. Так как строки не сохраняются в порядке поступления.

S
Stalker_RED, 2019-03-30
@Stalker_RED

SELECT `id`
FROM `tbl2`
ORDER BY `id` DESC
LIMIT 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question