I have a table that holds a list of tasks to be performed by a process. Each task only works on items that match the input status, and when the task is completed it changes the item's status to the output status.
To track these tasks I use a table like this.
CREATE TABLE `tasks` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(80) NOT NULL,
`job_type` varchar(45) NOT NULL,
`input_status` varchar(45) DEFAULT NULL,
`output_status` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
)
The task's statuses form a chain of events. Here is an example
NULL -> NEW
NEW -> CREATE
CREATE -> INSPECT
INSPECT -> VERIFY
VERIFY -> PUBLISH
In real life the list of tasks is very long. note: The types of different statuses are unknown to me, these user defined values.
When I view the table using an order of input_status
the records show the tasks in the wrong order. Sorting by input_status
and output_status
also doesn't work (obviously).
How can I sort the table where null
is first, followed by the chain of input_status
to output_status
?
I figure that I'll have to create a virtual field to hold an extra sorting value, but I'm not sure what it should be or calculated.
Here is what I've tried so far, but it doesn't work.
SELECT *,
(SELECT input_status FROM tasks AS parent
WHERE parent.output_status = tasks.input_status
) AS sorted
FROM tasks
ORDER BY sorted, input_status;