SELECT pstampJob.*,
    IFNULL(Label.priority, 10000) AS priority
FROM pstampJob
    JOIN pstampRequest USING(req_id)
    LEFT JOIN pstampDependent USING(dep_id)
    LEFT JOIN Label ON pstampRequest.label = Label.label
WHERE pstampRequest.state = 'run'
    AND pstampRequest.fault = 0
    AND pstampJob.state = 'run'
    AND pstampJob.fault = 0
    AND (dep_id = 0 OR pstampDependent.state = 'full')
    AND (Label.active OR Label.active IS NULL)
