我使用以下查询加入了两个表。它给了我重复,因为Table02有重复。我需要防止欺骗。
注意:
我不能在t1.NumberID上使用distinct。
SELECT t1.NumberID,t1.Copies,t1.NameStreet 从Table01 t1 加入Table02 t2 on t2.Name = LEFT(t1.NameStreet,Len(t2.Name))order by t1.NumberID,t1.Namestreet
Table01
ID NumberID Copies NameStreet
1 11111111 2 Julian Assange 100 Majestic street, Toronto
2 22222222 1 Brad Pit 200 Loaden Blvd, Toronto
3 11111111 0 Allen Sunderland 9, Unit 2, Janet Street, BC
TABLE02
ID Name
1 Julian Assange
2 Allen Sunderland
3 Julian Assange
结果: -
1 11111111 2 Julian Assange 100 Majestic street, Toronto
3 11111111 0 Allen Sunderland 9, Unit 2, Janet Street, BC
1 11111111 2 Julian Assange 100 Majestic street, Toronto
预期结果: -
1 11111111 2 Julian Assange 100 Majestic street, Toronto
3 11111111 0 Allen Sunderland 9, Unit 2, Janet Street, BC
答案 0 :(得分:2)
将此添加到查询的末尾:
GROUP BY t1.NumberID, t1.Copies, t1.NameStreet