题解 | 4种

查询2022年以来刷题用户的用user_id和毕业院校

https://www.nowcoder.com/practice/ea7d9e67f14e4cfc860bbbe9f65420a6

> exists

select
    t1.user_id,
    t1.university
from
    user_info t1
where exists (
    select 1 from questions_pass_record t2
    where t1.user_id = t2.user_id and year(t2.date) >= 2022
)

> join

select
    t1.user_id,
    t1.university
from
    user_info t1
    join questions_pass_record t2 on t1.user_id = t2.user_id
where 
    year(t2.date) >= 2022

> 如果数据记录表比较大

select
    t2.user_id,
    t2.university
from (
    select distinct user_id from questions_pass_record where year(date) >= 2022
) t1
join user_info t2 on t1.user_id = t2.user_id

select
    user_id,
    university
from
    user_info
where 
    user_id in (
        select distinct user_id from questions_pass_record where year(date) >= 2022
    )

全部评论

相关推荐

10 1 评论
分享
牛客网
牛客企业服务