首页 > 试题广场 >

查询培训指定课程的员工信息

[编程题]查询培训指定课程的员工信息
  • 热度指数:41750 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
某公司员工信息数据及员工培训信息数据如下:
员工信息表staff_tb(staff_id-员工id,staff_name-员工姓名,staff_gender-员工性别,post-员工岗位类别,department-员工所在部门),如下所示:
staff_id staff_name staff_gender post department
1 Angus male Financial dep1
2 Cathy female Director dep1
3 Aldis female Director dep2
4 Lawson male Engineer dep1
5 Carl male Engineer dep2
6 Ben male Engineer dep1
7 Rose female Financial dep2
员工培训信息表cultivate_tb(info_id-信息id,staff_id-员工id,course-培训课程),如下所示:
注:该公司共开设了三门课程,员工可自愿原则性培训0-3项;
info_id staff_id course
101 1 course1, course2
102 2 course2
103 3 course1, course3
104 4 course1, course2, course3
105 5 course3
106 6 NULL
107 7 course1, course2
问题:请查询培训课程course3的员工信息?
注:只要培训的课程中包含course3课程就计入结果
要求输出:员工id、姓名,按照员工id升序排序;
示例数据结果如下:
staff_id staff_name
3 Aldis
4 Lawson
5 Carl

解释:有员工3、4、5培训了course3课程,故结果如上
示例1

输入

drop table if exists  `staff_tb` ; 
CREATE TABLE `staff_tb` (
`staff_id` int(11) NOT NULL,
`staff_name` varchar(16) NOT NULL,
`staff_gender` char(8) NOT NULL,
`post` varchar(11) NOT NULL,
`department` varchar(16) NOT NULL,
PRIMARY KEY (`staff_id`));
INSERT INTO staff_tb VALUES(1,'Angus','male','Financial','dep1'); 
INSERT INTO staff_tb VALUES(2,'Cathy','female','Director','dep1'); 
INSERT INTO staff_tb VALUES(3,'Aldis','female','Director','dep2'); 
INSERT INTO staff_tb VALUES(4,'Lawson','male','Engineer','dep1'); 
INSERT INTO staff_tb VALUES(5,'Carl','male','Engineer','dep2'); 
INSERT INTO staff_tb VALUES(6,'Ben','male','Engineer','dep1'); 
INSERT INTO staff_tb VALUES(7,'Rose','female','Financial','dep2'); 

drop table if exists  `cultivate_tb` ;   
CREATE TABLE `cultivate_tb` (
`info_id` int(11) NOT NULL,
`staff_id` int(11) NOT NULL,
`course` varchar(32) NULL,
PRIMARY KEY (`info_id`));
INSERT INTO cultivate_tb VALUES(101,1,'course1,course2');
INSERT INTO cultivate_tb VALUES(102,2,'course2');
INSERT INTO cultivate_tb VALUES(103,3,'course1,course3');
INSERT INTO cultivate_tb VALUES(104,4,'course1,course2,course3');
INSERT INTO cultivate_tb VALUES(105,5,'course3');
INSERT INTO cultivate_tb VALUES(106,6,NULL);
INSERT INTO cultivate_tb VALUES(107,7,'course1,course2');

输出

staff_id|staff_name
3|Aldis
4|Lawson
5|Carl
select staff_id,staff_name from staff_tb
where staff_id 
in (
    select distinct staff_id
    from cultivate_tb
    where course like "%course3%"
)
order by staff_id asc

编辑于 2024-02-19 17:42:01 回复(3)
select c.staff_id,staff_name
from cultivate_tb c
left join staff_tb s on c.staff_id=s.staff_id
where course like '%course3%'
order by 1

发表于 2024-12-20 14:11:16 回复(0)
select staff_id, staff_name from 
(select s.staff_id staff_id, s.staff_name staff_name,
case when c.course like '%1%' then 1 else 0 end course1,
case when c.course like '%2%' then 1 else 0 end course2,
case when c.course like '%3%' then 1 else 0 end course3
from staff_tb s
left join cultivate_tb c on s.staff_id = c.staff_id) t1
where course3 = 1
思路:先将表处理成这个样子

然后去所需字段,过滤出课程3为1的行即可
发表于 2024-12-31 20:19:17 回复(3)
用模糊查询 企业里不允许前面有% 会拖垮整个系统.
发表于 2025-07-14 21:08:27 回复(0)
select c.staff_id,c.staff_name 
from cultivate_tb as c 
left join staff_tb as s on c.staff_id = s.staff_id 
where s.course like '%course3'
通过不了,算了不纠结了,应该只是网页出问题了
发表于 2024-08-22 16:23:03 回复(8)
select `staff_id`, `staff_name` from `staff_tb` where `staff_id` in (select `staff_id` from `cultivate_tb` where `course` like "%course3%") order by `staff_name`;
发表于 2024-05-20 22:09:02 回复(0)
select
    s.staff_id as staff_id,
    s.staff_name as staff_name
from
    staff_tb s
left join cultivate_tb c
    on s.staff_id = c.staff_id
    and c.course like '%course3%'
where
    c.staff_id is not null
order by
    s.staff_id;
发表于 2026-03-31 20:22:21 回复(0)
select
st.staff_id,staff_name
from staff_tb st
left join cultivate_tb ct
on  st.staff_id=ct.staff_id
where course like'%course3%'

发表于 2026-02-10 15:56:36 回复(0)
    c.staff_id,
    staff_name
FROM staff_tb s
JOIN cultivate_tb c USING (staff_id)
WHERE course LIKE '%course3%'
ORDER BY c.staff_id
发表于 2025-07-22 11:18:41 回复(0)
select staff_id,staff_name
from (
       select staff_id,staff_name
       from staff_tb) s 
right join (
             select staff_id from cultivate_tb
             where course like'%3%') c 
using(staff_id)
order by staff_id 

发表于 2025-03-22 16:39:53 回复(0)
select ct.staff_id,staff_name
from staff_tb st,cultivate_tb ct
where st.staff_id=ct.staff_id
and ct.course like '%3'
order by staff_id;
发表于 2025-03-17 16:14:34 回复(0)
这里代码不能格式化,格式化后反而不能通过
select st.staff_id, st.staff_name from cultivate_tb as ct left join staff_tb as st on ct.staff_id = st.staff_id where ct.course is not NULL and ct.course like '%course3%' order by st.staff_id


发表于 2024-08-19 15:28:52 回复(1)
with tmp
as(
select
a.staff_id
,a.staff_name
,c.course_name
from
staff_tb a
join
cultivate_tb b
on a.staff_id = b.staff_id
join
json_table(
concat('["',replace(b.course,',','","'),'"]'),
'$[*]'
columns( course_name varchar(50) path '$')
) c
)
select
staff_id
,staff_name
from tmp
where course_name = 'course3'
order by staff_id
;

发表于 2026-04-15 10:23:23 回复(0)
select staff_id,staff_name
from staff_tb
where staff_id in (
    select staff_id
    from cultivate_tb
    where course like '%course3%'
)
order by 1
发表于 2026-04-13 14:53:26 回复(0)
select
    c.staff_id,
    s.staff_name
from
    staff_tb s
    join cultivate_tb c on s.staff_id = c.staff_id
where
    #c.course like '%course3%'
    find_in_set('course3',course)
    #c.course like '%3%'
order by s.staff_id;


FIND_IN_SET

格式:FIND_IN_SET(要找的字符串, 逗号分隔的字符串)
作用:判断前面的字符串是否在后面的逗号列表里,存在返回位置数字,不存在返回 0。
举例
  • FIND_IN_SET('course3', 'course1,course2,course3')→ 返回 3
  • FIND_IN_SET('course3', 'course1,course2')→ 返回 0
在 WHERE 中
返回 >0 就代表存在,所以直接写:
WHERE FIND_IN_SET('course3', c.course)

发表于 2026-04-11 16:02:12 回复(0)
注意要使用模糊查询
select
    st.staff_id,
    st.staff_name
from
    staff_tb st
    join cultivate_tb ct on st.staff_id = ct.staff_id
where
    ct.course like '%course3%'
order by
    st.staff_id asc

发表于 2026-04-07 10:51:44 回复(0)
select 
    t1.staff_id,
    t1.staff_name
from staff_tb t1
join (
    select 
        staff_id,
        course_sub
    from (
        select 
            staff_id,
            course_sub
        from cultivate_tb
        LATERAL VIEW explode(split(course, ',')) tmp as course_sub
    ) a
    where course_sub = 'course3'
) t2
on t1.staff_id = t2.staff_id
order by t1.staff_id
Hive支持的爆炸函数语法

发表于 2026-04-02 23:44:50 回复(0)
SELECT 
    s.staff_id,
    s.staff_name
FROM staff_tb s
JOIN cultivate_tb c ON s.staff_id = c.staff_id
WHERE INSTR(c.course, 'course3') > 0
ORDER BY s.staff_id;

发表于 2026-04-02 16:30:35 回复(0)
select s.staff_id, s.staff_name
from staff_tb s left join cultivate_tb c
on s.staff_id = c.staff_id
where course like '%course3%'
order by s.staff_id

发表于 2026-03-29 10:51:05 回复(0)
SELECT
    a.staff_id,
    b.staff_name    
FROM cultivate_tb AS a
INNER JOIN staff_tb AS b ON a.staff_id = b.staff_id
    AND a.course LIKE '%course3%'
GROUP BY a.staff_id
ORDER BY a.staff_id;
发表于 2026-02-27 23:28:17 回复(0)