SELECT * FROM
    (SELECT 
        diffRun.diff_id,
        diffRun.skycell_id,
        diffRun.tess_id,
        0 as stack_id,
        warpSkyfile.warp_id,
        warpSkyfile.uri,
        warpSkyfile.path_base,
        diffInputSkyfile.template,
        rawExp.camera
    FROM diffRun
    JOIN diffInputSkyfile
        USING(diff_id)
    JOIN warpSkyfile
        ON  diffInputSkyfile.warp_id    = warpSkyfile.warp_id
        AND diffInputSkyfile.skycell_id = warpSkyfile.skycell_id
        AND diffInputSkyfile.tess_id    = warpSkyfile.tess_id
    JOIN warpRun
        ON diffInputSkyfile.warp_id = warpRun.warp_id
    JOIN camProcessedExp
        ON warpRun.cam_id = camProcessedExp.cam_id
    JOIN chipRun
        ON camProcessedExp.chip_id = chipRun.chip_id
    JOIN chipProcessedImfile
        ON chipRun.chip_id = chipProcessedImfile.chip_id
    JOIN rawExp
        ON chipProcessedImfile.exp_id = rawExp.exp_id
    WHERE
        diffRun.state = 'run'
    UNION
    SELECT 
        diffRun.diff_id,
        diffRun.skycell_id,
        diffRun.tess_id,
        stackSumSkyfile.stack_id,
        0 as warp_id,
        stackSumSkyfile.uri,
        stackSumSkyfile.path_base,
        diffInputSkyfile.template,
        rawExp.camera
    FROM diffRun
    JOIN diffInputSkyfile
        USING(diff_id)
    JOIN stackSumSkyfile
        ON  diffInputSkyfile.stack_id = stackSumSkyfile.stack_id
    JOIN stackInputSkyfile
        ON diffInputSkyfile.stack_id = stackInputSkyfile.stack_id
    JOIN warpRun
        ON stackInputSkyfile.warp_id = warpRun.warp_id
    JOIN camProcessedExp
        ON warpRun.cam_id = camProcessedExp.cam_id
    JOIN chipRun
        ON camProcessedExp.chip_id = chipRun.chip_id
    JOIN chipProcessedImfile
        ON chipRun.chip_id = chipProcessedImfile.chip_id
    JOIN rawExp
        ON chipProcessedImfile.exp_id = rawExp.exp_id
    WHERE
        diffRun.state = 'run'
    ) as Foo
