Sqlalchemy 如何实现 case + subquery 嵌套 subquery

2022-09-08 15:47:53 +08:00
 wuwukai007
select case
           when user.id > 10000 then
               (select true_flag
                from score
                where created_time = (
                    select max(created_time)
                    from score
                    where user.id = score.user_id
                ))
           when user.id <= 10000 then
               (select true_flag
                from score_bak
                where created_time = (
                    select max(created_time) from score_bak where user.id = score_bak.user_id
                ) 
               ) 
        end true_flag
from user;
1446 次点击
所在节点    Python
3 条回复
xhzhang
2022-09-08 20:56:37 +08:00
这个你可以把 case 语句放到 where 的 join 条件中,用 user join 两张表,添加不同的 join 条件,select 语句就按照你现在的逻辑取数就可以
xhzhang
2022-09-08 20:58:48 +08:00
select xxx from user join score on user.id = score.user_id and user.id > 10000 join score_bak on user.id = score.user_id and user.id <= 10000
0x0208v0
2022-09-09 09:58:16 +08:00
试了一下好像还真是和楼主说的会变成连表

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/878651

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX