Question Locked

Sql Statement for joins one to many

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

TableA

AId AName
---------------
1 AbC
2 Atable
3 ABus

TableB

BId BName
----------------
20 Bac
21 Bqw
22 Bws
23 Bqs

Table C

CId AId BId
---------------------
1 1 20
2 1 21
3 1 22
4 2 20
5 2 21
6 3 23
7 3 21

Let put it this way

AId BId
1 20,21,22,23
2 20,21
3 23,21

Forevery AId, there can be multiple BIds associated to it. So From UI Only the BID's will be send to the database. What I need to know is If the combination of BIds already associated to AID or it exists in the database. If they exist we need to send true or else we need to send false. say for this combination

BIDs

20,22 return false
20,21 return true
20,21,22 return false
23,21 return true

Any Ideas..

Thanks

3 replies

by slayer_

select field1 from table1, table2 where table1.primarykey = table2.primarykey

by scott_heath

I have no idea how you program accesses the data, if this is a one at a time check or a job that runs, but you could use this to start. More detail about how the job or process runs might help though

DECLARE @result bit -- 1 is true, 0 is false

IF 20 IN (SELECT Bld FROM TableC WHERE Ald = 1)
AND 22 IN (SELECT Bld FROM TableC WHERE Ald = 1)
BEGIN
@result = 0
END
ELSE
BEGIN
@result = 0
END

Regards,
Scott

http://scottisageek.spaces.live.com

This discussion is closed to new replies.