是否可以使用Distinct语句对表进行更新?我在这里有一个查询,它将映射另一个表中的条目,但table1有重复的条目。删除distinct语句会导致输出错误。
select distinct dm.[EE],dm.document_name,lu.*
from Table1 as dm
left join Table2 as lu on lu.[document_name]=dm.document_name
where Doc_Count=2
我在sql小提琴中制作了一个示例场景。 (http://sqlfiddle.com/#!6/634ef/1)
CREATE TABLE TABLE1
([EE] int, [Name] nvarchar(50),[Doc_Count] int, [document_name] nvarchar(50), [orginal_name] nvarchar(50));
INSERT INTO TABLE1
([EE], [Name],[Doc_Count], [document_name])
VALUES
(001, 'Employee 1','2','Admin123.pdf'),
(001, 'Employee 1','2', 'Admin123.pdf'),
(002, 'Employee 2','2', 'password.pdf'),
(002, 'Employee 2','2', 'password.pdf')
CREATE TABLE TABLE2
([document_name] nvarchar(50), [original_name] nvarchar(50));
INSERT INTO TABLE2
([document_name], [original_name])
VALUES
('Admin123.pdf','test444.pdf'),
('Admin123.pdf','test0124.pdf'),
('password.pdf','hello1.pdf'),
('password.pdf','hello2.pdf')
结果表:
EE document_name document_name original_name
1 Admin123.pdf Admin123.pdf test0124.pdf
1 Admin123.pdf Admin123.pdf test444.pdf
2 password.pdf password.pdf hello1.pdf
2 password.pdf password.pdf hello2.pdf
答案 0 :(得分:0)
这就是你要找的东西
SELECT * FROM TABLE2 INNER JOIN
(
SELECT DISTINCT [document_name] FROM TABLE1
)T ON TABLE2.Document_Name = T.Document_Name