I have the following table called 'tasks_processes':
id| task_id | process_id | start | end
1| 2 | 43 | 0 | 250
2| 2 | 82 | 250 | 500
3| 2 | 21 | 500 | 750
So, 'start' and 'end' columns represent bounds of range and are unique within the task. For new process I need perform INSERT SELECT query like this:
INSERT INTO tasks_processes (start,end,task_id,process_id)
SELECT
COALESCE ((SELECT MAX(end) FROM tasks_processes WHERE task_id=2),0) as new_val ,
new_val + 250,
2,
12;
In other words I need to insert value based on previously calculated value. But MySQL gives me DatabaseError: no such column: new_val
How to solve this problem?
You have to repeat the subquery, like this:
INSERT INTO tasks_processes (start,end,task_id,process_id)
SELECT COALESCE ((SELECT MAX(end) FROM tasks_processes WHERE task_id=2),0),
COALESCE ((SELECT MAX(end) FROM tasks_processes WHERE task_id=2),0) + 250,
2,
12;
or you can use something like this:
INSERT INTO tasks_processes (start,end,task_id,process_id)
SELECT new_val, new_val+250, 2, 12
FROM (SELECT coalesce(MAX(end), 0) as new_val FROM tasks_processes WHERE task_id=2) s;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With