优化查询重复

时间:2015-02-08 19:54:51

标签: mysql

如何从此查询中删除重复内容?

SELECT
    *, UNIX_TIMESTAMP(
        STR_TO_DATE(
            CONCAT(
                IFNULL(expected_release_day, 1),
                ' ',
                IFNULL(
                    expected_release_month,
                    IFNULL(
                        3 * expected_release_quarter,
                        1
                    )
                ),
                ' ',
                expected_release_year
            ),
            '%d %m %Y'
        )
    ) calc
FROM
    games
WHERE
    expected_release_year > 0 && UNIX_TIMESTAMP(
        STR_TO_DATE(
            CONCAT(
                IFNULL(expected_release_day, 1),
                ' ',
                IFNULL(
                    expected_release_month,
                    IFNULL(
                        3 * expected_release_quarter,
                        1
                    )
                ),
                ' ',
                expected_release_year
            ),
            '%d %m %Y'
        )
    ) > UNIX_TIMESTAMP(NOW())
ORDER BY
    calc

1 个答案:

答案 0 :(得分:1)

一种方法是使用子查询

SELECT *,calc FROM
(
    SELECT *, UNIX_TIMESTAMP(
        STR_TO_DATE(
            CONCAT(
                IFNULL(expected_release_day, 1),
                ' ',
                IFNULL(
                    expected_release_month,
                    IFNULL(
                        3 * expected_release_quarter,
                        1
                    )
                ),
                ' ',
                expected_release_year
            ),
            '%d %m %Y'
        )
    ) calc
FROM
    games)x
WHERE
    expected_release_year > 0 && calc > UNIX_TIMESTAMP(NOW())
ORDER BY
    calc