I
I
iron eagle2016-04-08 23:25:08
MySQL
iron eagle, 2016-04-08 23:25:08

Is it possible to do SELECT and INSERT in one query?

There are 2 simple requests, is it possible to send them at the same time as one?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Kovalsky, 2016-04-08
@dmitryKovalskiy

You can execute a stored procedure and do whatever you want. or to make within one transaction - two scripts.

D
Dmitry, 2016-04-08
@kashamalasha

BEGIN
  SAVEPOINT bak_trn; -- опционально
  SELECT FROM ... ;
  INSERT INTO ... ;
EXCEPTION  -- опционально
   WHEN OTHERS THEN
      ROLLBACK TO bak_trn;
      RAISE;
END;
COMMIT;

If you don’t feel sorry for anything, and there is nothing to break, you can do it without a save and without a rollback.

M
Mirocow, 2016-04-09
@mirocow

Example 1
UPDATE
Table_A
SET
Table_A.token = CONCAT_WS(':', Table_B.category_id, Table_B.form_id, Table_B.mark_id, Table_B.model_id)
FROM
tbl_category_mark_model_form Table_A
INNER JOIN
tbl_category_mark_model_form Table_B
ON
Table_A.id = Table_B.id
;
Example 2
UPDATE tbl_category_mark_model_form AS tbl_1
INNER JOIN(
SELECT * FROM tbl_category_mark_model_form
) AS tbl_2 ON tbl_1.id = tbl_2.id
SET tbl_1.token = CONCAT_WS(':', tbl_2.category_id, tbl_2.form_id, tbl_2.mark_id )
;
other examples:docs.mirocow.com/doku.php?id=sql:mysql:update-someself

I
igruschkafox, 2016-04-14
@igruschkafox

in MS SQL there is Output
returns the rows, including the assigned IDs and guides ;)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question