Mysql: select, insert, delete and update in a single query

I need to use select, insert, delete and update in one request.

(I need to copy the data from the old table to the new one, and then delete the old ones and update another).

Paste and select (copy function that I was able to, but now I have a problem)

I have this query:

INSERT INTO news_n (id, data)
    SELECT (id, data)
    FROM news
    WHERE id > 21

thank

+3
source share
4 answers

You cannot do everything in one request , but you can do it all in one transactionif you use a transactional storage engine (e.g. InnoDB). It may be what you want, but it’s hard to say only using the information you provided in your question.

START TRANSACTION;

INSERT...;
DELETE...
UPDATE...;

COMMIT;
+5

MySQL MERGE, :

INSERT
INTO    news_n (id, data)
SELECT  id, data
FROM    news
WHERE   id > 21
ON DUPLICATE KEY UPDATE
SET     data = news.data

DELETE
FROM    news_n
WHERE   id NOT IN
        (
        SELECT  id
        FROM    news
        WHERE   id > 21
        )

PRIMARY KEY (id) .

+1

, .

, ,

+1

Select/Update/etc . , .

0

Source: https://habr.com/ru/post/1772885/


All Articles