gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:41:46 24 4
gpt4 key购买 nike

假设我有这个查询:`

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

因此,在这个示例中,我有超过 1 个别名,并且我想在其他别名中使用其中一些,就像嵌套别名一样。

如何在不将别名复制/粘贴到另一个别名中的情况下实现这一点,例如:

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

注意确定上面的语法是否正确。

最佳答案

试试这个:

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 结果将为 YN,并且您在下一个测试中检查 Yes结果为 false,并且所有行的 firstNested 都将 Not 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

关于php - 如何使用嵌套别名让查询更简单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49968737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com