从连接的两个表中获取前两个唯一记录 - Mysql
Posted
技术标签:
【中文标题】从连接的两个表中获取前两个唯一记录 - Mysql【英文标题】:Getting first two unique records from two table joined - Mysql 【发布时间】: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 中的行代表无序集,你能定义“前两个”吗 对不起,我不明白 【参考方案1】:您可以在 MySQL 8+ 中使用窗口函数:
select *
from financial_details bsd inner join
(select fi.*,
row_number() over (partition by emp_id order by id) as seqnum
from performance_appraisal fi
) fi
on fi.emp_id = bsd.emp_id and seqnum <= 2;
【讨论】:
您好,谢谢,我收到语法错误,因为缺少右括号。使用XAMMP,我的Mysql版本是innodb_version 5.6.28-76.1,版本10.1.13-MariaDB。我该如何解决旧版本?【参考方案2】:这里有一些需要考虑的事情,尽管如前所述,此解决方案仅适用于 8.0 之前的版本...
SELECT emp_id
, id
FROM
( SELECT emp_id
, id
, CASE WHEN @prev = emp_id THEN @i:=@i+1 ELSE @i:=1 END i
, @prev:=emp_id prev
FROM financial_details
, (SELECT @prev:=null,@i:=0) vars
ORDER
BY emp_id
, id
) x
WHERE i <= 2;
+--------+------+
| emp_id | id |
+--------+------+
| 1 | 1 |
| 1 | 2 |
| 2 | 5 |
| 2 | 6 |
+--------+------+
【讨论】:
您好,非常感谢。它对一个表有帮助,但是在使用连接时,我如何为这些数据添加另一个表 performance_appraisal。 继续。尝试一下。【参考方案3】:感谢Strawberry 回答,我们需要在 where 子句中添加我的输出。
select * from financial_details bsd inner join performance_appraisal fi on fi.emp_id = bsd.emp_id where bsd.id in (SELECT id
FROM ( SELECT emp_id, id, CASE WHEN @prev = emp_id THEN @i:=@i+1 ELSE @i:=1 END i, @prev:=emp_id prev
FROM financial_details, (SELECT @prev:=null,@i:=0) vars ORDER BY emp_id, id
) x
WHERE i <= 2);
【讨论】:
以上是关于从连接的两个表中获取前两个唯一记录 - Mysql的主要内容,如果未能解决你的问题,请参考以下文章