M
M
Mykola2014-12-07 01:07:01
MySQL
Mykola, 2014-12-07 01:07:01

How to write an INSERT-SELECT query?

In general, it is necessary to add a new value to the old balance indicator.
Query where is current balance running:

INSERT INTO `test_db_vytrat` (`balans`) select balans+1000 from test_db_vytrat where user_id = 1 ORDER BY `id` DESC LIMIT 0 , 1;

But in addition to the balance, I need to write down other values, and here is the problem.
But on request:
INSERT INTO `test_db_vytrat` (`id`, `user_id`, `data`, `cat`, `oper`, `suma_oper`, `balans`, `note`) VALUES (NULL, '$_POST[uid]', '$_POST[data]', '$_POST[category]', '$_POST[type]', '$_POST[suma]', select balans+1000 from test_db_vytrat where user_id = 1 ORDER BY `id` DESC LIMIT 0 , 1, '$_POST[note]');

SQL swears.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WebSpider, 2014-12-07
@iSensetivity

INSERT INTO `test_db_vytrat` (`id`, `user_id`, `data`, `cat`, `oper`, `suma_oper`, `balans`, `note`) 
SELECT NULL as id, '{$_POST[uid]}' as user_id, '{$_POST[data]}' as data, ..., balans+1000 as balans, '{$_POST[note]}' as note from test_db_vytrat where user_id = 1 ORDER BY `id` DESC LIMIT 0 , 1;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question