我有一个数据库表,用于存储员工的ID以及他们之前正在进行的项目。现在,我想要检索一对在这两个员工之间处理相同项目和共同项目的员工。 如果我做“自我加入”的方法,那么我会得到重复的行。
SELECT DISTINCT ep1.employee_id, ep2.employee_id, COUNT(p.id)
FROM employee_project ep1, employee_project ep2, project p
WHERE ep1.project_id=ep2.project_id
AND ep1.employee_id ep2.employee_id
AND p.id=ep1.project_id
GROUP BY ep1.employee_id, ep2.employee_id, p.id
结果:
employee1 | employee2 | 5
employee2 | employee1 | 5
答案 0 :(得分:2)
将ep1.employee_id >= ep2.employee_id
添加到where子句。
答案 1 :(得分:0)
我会创建一个新表,其中包含要保持唯一的列的唯一索引。然后从旧表插入到新表中,忽略有关重复行的警告。最后,我会删除(或重命名)旧表并将其替换为新表。在MySQL中,这看起来像
CREATE TABLE tmp LIKE mytable;
ALTER TABLE tmp ADD UNIQUE INDEX myindex (emp_name, emp_address, sex, marital_status);
INSERT IGNORE INTO tmp SELECT * FROM mytable;
DROP TABLE mytable;
RENAME TABLE tmp TO mytable;
答案 2 :(得分:0)
另一种选择是“规范化”组合并对其进行分组:
SELECT greatest(ep1.employee_id, ep2.employee_id),
least(ep1.employee_id, ep2.employee_id),
count(*)
FROM employee_project ep1
JOIN employee_project ep2
ON ep1.project_id=ep2.project_id
AND ep1.employee_id=ep2.employee_id
JOIN project p ON p.id=ep1.project_id
GROUP BY greatest(ep1.employee_id, ep2.employee_id),
least(ep1.employee_id, ep2.employee_id)
答案 3 :(得分:-1)
看起来所有四列值都是重复的,所以你可以这样做 -
select distinct emp_name, emp_address, sex, marital_status
from YourTable
但是,如果婚姻状况可能不同,并且您有一些其他列可供选择(例如,您希望基于列create_date的最新记录),则可以执行此操作
select emp_name, emp_address, sex, marital_status
from YourTable a
where not exists (select 1
from YourTable b
where b.emp_name = a.emp_name and
b.emp_address = a.emp_address and
b.sex = a.sex and
b.create_date >= a.create_date)