【发布时间】:2019-10-08 22:23:40
【问题描述】:
表 1:employee_detail:
id name
1 ABC
2 CCC
3 FFF
4 ggg
5 jjj
表 2:性能评估
id date_of_join isAppraisalcomplete emp_id
1 1-07-2010 Yes 1
2 09-6-2010 Yes 2
3 10-7-2012 Yes 3
4 23-8-2015 No 4
5 07-11-2018 No 5
表 3:财务细节
id salary hike emp_id p_a_id year
1 11000 12 1 1 2016
2 11000 9 1 1 2017
3 11000 11 1 1 2016
4 11000 10 1 1 2017
2 33000 15 2 2 2016
3 36000 10 2 2 2017
4 31000 15 2 2 2016
5 44001 10 2 2 2017
..........
..........
..........
期待输出:
Emp_id Date_of_join isAppraisalcomplete Salary Hike year
1 1-07-2010 Yes 11000 12 2016
1 1-07-2010 Yes 11000 9 2017
2 09-06-2010 Yes 33000 15 2016
2 09-06-2010 Yes 36000 10 2017
..........
..............
查询我用于:
select * from financial_details bsd inner join performance_appraisal fi on fi.emp_id = bsd.emp_id limit 2;
我的结果:
Emp_id Date_of_join isAppraisalcomplete Salary Hike year
1 1-07-2010 Yes 11000 12 2016
1 1-07-2010 Yes 11000 9 2017
在增加限制时,它会显示来自 emp_id 的所有记录,比如 emp_id 1,不仅是前两条记录,还包括所有记录。
如何从表中获取前两条记录并使用 MySql 加入另一个表。
在使用 where 条件时,按 desc 排序,限制为 2,我得到一条记录(一名员工)的准确结果。但实际上我试图从 Financial_details 表和 performance_appraisal 表连接中获取前两个唯一数据的所有记录(所有员工)。请帮忙。
编辑:
查询:
CREATE TABLE employee_detail
(
id int,
name varchar(255)
);
CREATE TABLE performance_appraisal
(
id int,
date_of_join varchar(255),
isAppraisalcomplete varchar(255),
emp_id int
);
CREATE TABLE financial_details
(
id int,
salary varchar(255),
hike varchar(255),
emp_id int,
p_a_id int,
t_year varchar(255)
);
insert into employee_detail (id, name) values (1,"abc");
insert into employee_detail (id, name) values (2,"def");
insert into employee_detail (id, name) values (3,"ghi");
insert into performance_appraisal (id, date_of_join, isAppraisalcomplete, emp_id) values (1, "1-07-2010", "Yes", 1);
insert into performance_appraisal (id, date_of_join, isAppraisalcomplete, emp_id) values (2, "09-6-2010", "Yes", 2);
insert into performance_appraisal (id, date_of_join, isAppraisalcomplete, emp_id) values (3, "10-7-2012", "Yes", 3);
insert into performance_appraisal (id, date_of_join, isAppraisalcomplete, emp_id) values (4, "23-8-2015", "No", 4);
insert into performance_appraisal (id, date_of_join, isAppraisalcomplete, emp_id) values (5, "07-11-2018", "No", 5);
insert into financial_details (id, salary, hike, emp_id,p_a_id, t_year) values (1, "11000", "12", 1,1,"2016");
insert into financial_details (id, salary, hike, emp_id,p_a_id, t_year) values (2, "12000", "9", 1,1,"2017");
insert into financial_details (id, salary, hike, emp_id,p_a_id, t_year) values (3, "10500", "11", 1,1,"2016");
insert into financial_details (id, salary, hike, emp_id,p_a_id, t_year) values (4, "11400", "10", 1,1,"2017");
insert into financial_details (id, salary, hike, emp_id,p_a_id, t_year) values (5, "36000", "15", 2,2,"2016");
insert into financial_details (id, salary, hike, emp_id,p_a_id, t_year) values (6, "36000", "15", 2,2,"2017");
insert into financial_details (id, salary, hike, emp_id,p_a_id, t_year) values (7, "31000", "15", 2,2,"2016");
insert into financial_details (id, salary, hike, emp_id,p_a_id, t_year) values (8, "44000", "15", 2,2,"2017");
【问题讨论】:
-
嗨@Strawberry 请看这个链接。 db-fiddle.com/#&togetherjs=wFu5SlHtVN
-
谢谢我添加了问题
-
抱歉 - 记住 RDBMS 中的行代表无序集,你能定义“前两个”吗
-
对不起,我不明白
标签: mysql sql inner-join