E
E
entermix2015-12-08 22:37:48
MySQL
entermix, 2015-12-08 22:37:48

Is it possible to do UPDATE + INSERT in one query?

Made a request:

UPDATE `users` SET `balance` = balance + 11 WHERE `id` = '1' AND (INSERT INTO `payments` (`status_id`, `user_id`, `description`, `currency_id`, `amount`, `bonus_amount`, `sum`) VALUES ('2', '1', 'TEST', 643, 11, 22, 11)) LIMIT 1

An error occurs while executing:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO `payments` (`status_id`, `user_id`, `description`, `currency_id`, `amo' at line 1

Separate requests work. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2015-12-08
@entermix

You can't do that.
Break the query into 2 parts: INSERT separately, UPDATE separately; and wrap it in a transaction.
It is better to calculate the user's balance with a request from the payments table than just plus.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question