如何使用嵌套别名使查询更简单?

j8ag8udp  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(342)

假设我有一个问题:`

SELECT
    IF(uPer="Yes", "Y", "N") AS Manage,
    IF(date_check="Yes", "Y", "N") As dCheck,
    IF(dCheck="Yes", "Great", "Not Great") AS firstNested,
    IF(firstNested="Great",1,0) AS secondNested
FROM table

所以在这个例子中,我有不止一个别名,我想在其他别名中使用其中的一些,比如嵌套的别名。
如何在不将别名复制/粘贴到另一个别名(如:

IF(
   IF(dCheck="Yes", "Great", "Not Great") AS firstNested) ="Great",1,0
) AS secondNested

请注意,如果以上是正确的语法。

lkaoscv7

lkaoscv71#

试试这个:

SELECT
    Manage,
    dCheck,
    firstNested,
    IF(firstNested="Great",1,0) AS secondNested
FROM
(
    SELECT
        Manage,
        dCheck,
        IF(dCheck="Yes", "Great", "Not Great") AS firstNested
    FROM
    (
        SELECT
            IF(uPer="Yes", "Y", "N") AS Manage,
            IF(date_check="Yes", "Y", "N") As dCheck
            FROM table
    ) as t1
) as t2

缺陷: dCheck 结果将是 Y 或者 N 你在检查吗 Yes 在下一个测试中,哪个结果是假的 firstNestedNot Great 所有的行。
您的查询应该是:

SELECT
    Manage,
    dCheck,
    firstNested,
    IF(firstNested="Great",1,0) AS secondNested
FROM
(
    SELECT
        Manage,
        dCheck,
        IF(dCheck="Y", "Great", "Not Great") AS firstNested
    FROM
    (
        SELECT
            IF(uPer="Yes", "Y", "N") AS Manage,
            IF(date_check="Yes", "Y", "N") As dCheck
            FROM table
    ) as t1
) as t2

相关问题