How to refactor this MySQL code?

Posted by Jader Dias on Stack Overflow See other posts from Stack Overflow or by Jader Dias
Published on 2010-04-20T19:33:26Z Indexed on 2010/04/20 19:43 UTC
Read the original article Hit count: 221

Filed under:
|
SELECT *
(
    SELECT *
    FROM `table1`
    WHERE `id` NOT IN (
        SELECT `id` FROM `table2`
        WHERE `col4` = 5
    )
    group by `col2` having sum(`col3`) > 0
UNION
    SELECT *
    FROM `table1`
    WHERE `id` NOT IN (
        SELECT `id` FROM `table2`
        WHERE `col4` = 5
    )
    group by `col2` having sum(`col3`) = 0
) t1;

For readability and performance reasons, I think this code could be refactored. But how?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about refactoring