ADO/ADO.NET - how to join two table and select record for below requirement

Asked By mani on 20-Apr-12 07:19 AM
hi i two table called  sales and details..

    sales
   
   sareeno   inch   meter
   s4             12   29
   s5             18   30
   s6       13   14
   s4             12     29
   s5             18     30
  

 details

   sareeno   rate    discount
   s4           1000    10
   s5           20000   20
   s1           9000   30


here i need to select  same saree number record from second table only and also inch and meter from from first tble as
only as follows.any help please??i used inner join but getting duplication record from frist table also..not like that..but as folows..

    sareeno   rate   discount  inch  meter
   s4              1000   10    12    29
   s5            20000    20    18    40
Chintan Vaghela replied to mani on 20-Apr-12 07:26 AM

Hello,

 

Try following way

 

select  d.sareeno,   d.rate,   d.discount,  s.inch,  s.meter

from

    sales as s

        inner join

    details as d on s.sareeno = d.sareeno

 

Hope this helpful!

Thanks

 

 

Web Star replied to mani on 20-Apr-12 07:41 AM
You need to join on common column in both table with Inner join that will give you desired result


select Distinct  x.same, x.saree, x.discount  y.inch, y.meter

from

    sales as x

      inner join

    details as y on x.sareeno = y.sareeno



Only you must use Distinct keyword in select query to avoid duplicate record because your Sales table having duplicate record
Somesh Yadav replied to mani on 20-Apr-12 07:56 AM

You can write left outer join between this two tables Best way to understand is check the below image

Query for your requirement

SELECT A.uid, A.name, B.address FROM A LEFT JOIN B ON A.uid=B.uid 

Reading this original article on The Code Project will help you a lot: http://www.codeproject.com/KB/database/Visual_SQL_Joins.aspx.