mysql 如何在没有AS的情况下重写此查询?

ie3xauqp  于 2022-11-28  发布在  Mysql
关注(0)|答案(1)|浏览(124)

我正在完成一个HackerRank挑战,但文档中说我不应该使用AS关键字:

我需要在MySQL中重写此查询,以便它不包括WITH A AS中的AS,也不包括AS in SELECT...AS test

WITH A AS (
    SELECT DISTINCT
        MAX( LENGTH( customer_id ) ) AS test
    FROM
        orders

    UNION

    SELECT DISTINCT
        MIN( LENGTH( customer_id ) )
    FROM
        orders
)
SELECT
    test,
    LENGTH(test)
FROM
    A
4xrmg8kj

4xrmg8kj1#

WITH子句用于声明一个VIEW,因此可以如下重写它

SELECT
    test,
    LENGTH(test)
FROM
    (
 SELECT DISTINCT
        MAX( LENGTH( customer_id ) ) AS test
    FROM
        orders

    UNION

    SELECT DISTINCT
        MIN( LENGTH( customer_id ) )
    FROM
        orders)

相关问题