use Distinct Or Group by according to your requirement..
create table tbl1
(
col1 int,
col2 varchar(10),
col3 varchar(10)
)
insert into tbl1 values(1,'aaa','bbb'),(2,'aaa','bbb'),(3,'bbb','ccc'),(4,'bbb','ccc'),(5,'ccc','xxx')
create table tbl2
(
col1 varchar(10),
col2 varchar(10),
)
insert into tbl2 values('aaa','hello'),('bbb','Hi')
----General left query---
select t1.col2,t1.col3,t2.col2 from tbl1 t1
left join tbl2 t2
on t1.col2=t2.col1
result:
col2 col3 col2
aaa bbb hello
aaa bbb hello
bbb ccc Hi
bbb ccc Hi
ccc xxx NULL
---- using distinct----
select distinct t1.col2,t1.col3,t2.col2
from tbl1 t1
left join tbl2 t2
on t1.col2=t2.col1</pre>
result:
col2 col3 col2
aaa bbb hello
bbb ccc Hi
ccc xxx NULL
---Using Group by---
select t1.col2,t1.col3,t2.col2
from tbl1 t1
left join tbl2 t2
on t1.col2=t2.col1
group by t1.col2,t1.col3,t2.col2
Result:
col2 col3 col2
aaa bbb hello
bbb ccc Hi
ccc xxx NULL