MySQL SELECT CASE WHEN 某事然后返回 null

发布于 2024-10-11 03:47:35 字数 1484 浏览 1 评论 0原文

who

wid--name-------father---mother  
1----Daisy------David----Liza  
2----Jenny------Joe------Judy  
3----Meggy------Mike-----Manuela  
4----Sarah------Joe------Judy  
5----Chelsea----Bill-----Hillary  
6----Cindy------David----Liza    
7----Kelly------Joe------Judy 

ages

aid---whoid---age  
1-----1--------0  
2-----2--------0  
3-----3-------14  
4-----4-------30  
5-----5-------22  
6-----6-------17  
7-----1-------18  

我想要该列表作为结果:

id---name------age  
1----Meggy-----14  
2----Cindy-----17  
3----Daisy-----18 (Selected data that bigger than 0)  
4----Chelsea---22  
5----Sarah-----30  
6----Jenny-----30 (Her age is 0 on ages table and Sarah's age with same father and mother)  
7----Kelly-----30 (No data on ages table and Sarah's age with same father and mother)

我尝试了该查询:

SELECT 
    *,  
    (CASE age  
        WHEN '0' THEN (
            SELECT age 
            FROM ages a 
            LEFT JOIN who w 
                ON w.wid = a.whoid 
            WHERE 
                w.father = father 
                AND 
                w.mother = mother 
            ORDER BY a.age DESC LIMIT 1
        )  
        ELSE age  
    END
    ) AS newage  
FROM who  
LEFT JOIN ages 
    ON wid = whoid  
ORDER BY  newage

有什么问题吗?

Table who

wid--name-------father---mother  
1----Daisy------David----Liza  
2----Jenny------Joe------Judy  
3----Meggy------Mike-----Manuela  
4----Sarah------Joe------Judy  
5----Chelsea----Bill-----Hillary  
6----Cindy------David----Liza    
7----Kelly------Joe------Judy 

Table ages

aid---whoid---age  
1-----1--------0  
2-----2--------0  
3-----3-------14  
4-----4-------30  
5-----5-------22  
6-----6-------17  
7-----1-------18  

I want that list as a result:

id---name------age  
1----Meggy-----14  
2----Cindy-----17  
3----Daisy-----18 (Selected data that bigger than 0)  
4----Chelsea---22  
5----Sarah-----30  
6----Jenny-----30 (Her age is 0 on ages table and Sarah's age with same father and mother)  
7----Kelly-----30 (No data on ages table and Sarah's age with same father and mother)

I tried that query:

SELECT 
    *,  
    (CASE age  
        WHEN '0' THEN (
            SELECT age 
            FROM ages a 
            LEFT JOIN who w 
                ON w.wid = a.whoid 
            WHERE 
                w.father = father 
                AND 
                w.mother = mother 
            ORDER BY a.age DESC LIMIT 1
        )  
        ELSE age  
    END
    ) AS newage  
FROM who  
LEFT JOIN ages 
    ON wid = whoid  
ORDER BY  newage

What's wrong with that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烛影斜 2024-10-18 03:47:35

CASE … WHEN NULL 永远不会匹配任何内容,并且 CASE NULL 将始终匹配 ELSE 子句(在您的情况下返回 age< /code>,即NULL)。

使用此:

CASE COALESCE(age, 0) WHEN 0 THEN … ELSE age END

更新:

您还需要为表添加别名并在字段描述中使用别名:

SELECT  *,
        CASE COALESCE(age, 0)
        WHEN '0' THEN
                (
                SELECT  MAX(age)
                FROM    who wi
                JOIN    ages ai
                ON      ai.whoid = wi.wid
                WHERE   wi.father = w.father
                        AND wi.mother = w.mother
                )
        ELSE
                age
        END AS newage
FROM    who w
LEFT JOIN
        ages a
ON      a.whoid = w.wid
ORDER BY
        newage

CASE … WHEN NULL will never match anything, and CASE NULL will always match the ELSE clause (which in your case returns age, i. e. NULL).

Use this:

CASE COALESCE(age, 0) WHEN 0 THEN … ELSE age END

Update:

You also need to alias your tables and use the aliases in the field descriptions:

SELECT  *,
        CASE COALESCE(age, 0)
        WHEN '0' THEN
                (
                SELECT  MAX(age)
                FROM    who wi
                JOIN    ages ai
                ON      ai.whoid = wi.wid
                WHERE   wi.father = w.father
                        AND wi.mother = w.mother
                )
        ELSE
                age
        END AS newage
FROM    who w
LEFT JOIN
        ages a
ON      a.whoid = w.wid
ORDER BY
        newage
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文