Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 intersect

Author  Topic 

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-05-25 : 04:19:49
hi i have a question about intersect

assuming i have 2 table
tableA
custid | custname
1 | kim
2 | mary
3 | john

tableB
trxno | custid
5 | 1
6 | 2
7 | 4

how to get the following data by using intersect?
5 | 1 | kim
6 | 2 | mary
i have around 5 table to intersect with...jz gimi a guide on using intersect will be helpful >"< thx

nr
SQLTeam MVY

12543 Posts

Posted - 2009-05-25 : 04:22:59
select a.custid, b.trxno, a.custname
from TableA a
join TableB b
on a.custid = b.custid


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-05-25 : 04:24:10
select b.trxno,a.custid, a.custname from tablea a
inner join tableb b on b.custid = a.custid
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-05-25 : 04:25:23
thx thx...i think i won able to use intersect..xD
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-05-25 : 04:48:05
while using the intersect,union,except the columns should be equal datatype in the both tables then only we can use intersect,union,except

for this u can use this one
select custid from @t intersect select custid from @b
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2009-05-25 : 04:54:24
Why do you want use intersect?
It will only give the custid's then you would have to join to the tables to get the resultset anyway.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-05-25 : 05:23:37
coz when i read the instruction, there written something like returning data which included in both table..so i thought must use intersect x.x
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2009-05-25 : 05:36:11
Nope - that's a join as in my first post.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -