题解 | #列表 A,B中,对B中b1,b2对应同一个A中的a1的情况计数#

某乎问答回答过教育类问题的用户里有多少用户回答过职场类问题

http://www.nowcoder.com/practice/b02cf9ee7b9f4cdda308f8155ff3415d

本题是比较经典的运用题,现实工作中比较常见,思路和方法有很多,以下仅供参考:

方法一:使用group_concat函数,找出由author_id分组合并issue_type后的字符串中同时包含'Education'和'Career'的人数。需要使用locate函数,当字符串不存在于给定的字符中时,结果返回0

select count(*) from
(select group_concat(distinct issue_type) s
from issue_tb ib,answer_tb at
where ib.issue_id=at.issue_id
group by author_id
having locate('Education',s)!=0 and locate('Career',s)!=0)a

方法二:使用with as创建临时表,从临时表中分别选出包含'Education'与'Career'的author_id,将两个author_id联合比较即可得出同时参加的人数

with x as (select distinct author_id,issue_type from 
issue_tb ib,answer_tb at
where ib.issue_id=at.issue_id)
select count(*) num from
(select distinct author_id s1 from x
where issue_type='Education')a join 
(select distinct author_id s2 from x
where issue_type='Career')b
on s1=s2

方法三:先找进行issue_type,author_id分组查询,记为表a,其中issue_type为'Education'或者'Career';再将表a的author_id进行分组计数,再选择计数大于2的author_id进行计数即可

select count(*) num from
(select author_id,count(*) s from(
select issue_type,at.author_id from answer_tb at,issue_tb it
where at.issue_id=it.issue_id and (issue_type='Education' or issue_type='Career')
group by issue_type,author_id) a
group by author_id
having s>1)b

方法四:选出issue_type为'Career'的distinct(author_id),再以子查询中issue_type为'Education'限定author_id,再进行count即可

select count(distinct(author_id)) num
from answer_tb at1 ,issue_tb it
where at1.issue_id=it.issue_id and issue_type='Career' and
at1.author_id in
(select distinct(author_id) from answer_tb at,issue_tb it
where at.issue_id=it.issue_id and issue_type='Education')
全部评论
如果能有性能的分析就更好了
1 回复 分享
发布于 2022-08-19 18:36 广东
from后是子表,需要加上表别名
点赞 回复 分享
发布于 2023-02-23 20:55 英国
方法一为啥最后需要加一个a呀
点赞 回复 分享
发布于 2023-01-05 09:40 湖南
太妙了,赞!
点赞 回复 分享
发布于 2022-08-19 18:34 广东

相关推荐

不愿透露姓名的神秘牛友
06-23 18:34
点赞 评论 收藏
分享
04-29 22:35
门头沟学院 Java
牛友说改了名字能收到offer:旧图新发查看图片
点赞 评论 收藏
分享
家人们,我现在真的好纠结。我是26届的,目前还没有实习过。我现在的情况是,想参加秋招,但是感觉自己的简历特别空,没有实习经历会不会秋招直接凉凉啊?可我又听说现在很多公司对26届实习生也不太感冒,说什么不确定性大。而且我最近在准备考公,时间上也有点冲突。要是把时间花在实习上,备考时间就少了。但要是不实习,又怕以后就业有问题😫有没有懂行的友友帮我分析分析:26届现在不实习,秋招找工作真的会很难吗?考公和实习该怎么平衡啊?如果现在不实习,考完公再去找实习还来得及吗?真的太焦虑了,希望大家能给我点建议🙏
小破站_程序员YT:我可能和大家的观点不一样。人的精力是有限的,不能既要还要。你又想实习又想考公最后又要秋招上岸,我觉得哪有那么多的选择。你如果想考上岸,那就全力以赴。如果想秋招上岸,就继续投实习,投没了,就继续准备秋招,秋招不行继续春招。别到最后,考公没上岸,觉得是花了时间浪费在找实习上了, 秋招没上岸,觉得是浪费时间准备考公去了。我是认为很难说可以去平衡 不喜勿喷,可以叫我删除
点赞 评论 收藏
分享
评论
20
5
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务