geek
geek

Reputation: 816

fetch value from multiple table

I have a table t1:

c1 c2 
1  a
2  b
3  a

table t2:

c3  c4
4    1
5    2
6    3

I am writing a procedure:

select c1 from t1 where c2 = 'a'; 

which will give multiple outputs. Use that output (1,3)

select c3 from t2 where c4 = 1,

select c3 from t2 where c4 = 3 -- out put of first query;

How do I implement it ? Should i use any loops? If so, how do I loop or how do I write a procedure which returns the result of it?

Upvotes: 0

Views: 64

Answers (4)

Hemanth Krishna
Hemanth Krishna

Reputation: 76

select c1,c3 from t1,t2
where t1.c1=t2.c4 and  c2='a' 

Upvotes: 0

CompEng
CompEng

Reputation: 7376

You can use in

select c3 from t2 where c4 in
(select c1 from t1 where c2 = 'a')

Upvotes: 1

Zee786
Zee786

Reputation: 93

You might have to work more on it but this is how i think this can be implemented.Inside the SP create a table data type where you can insert the multiple values fetched and then you can pass these values to another SP as a table value parameters where you can do the rest of the operations.

Upvotes: 1

Fahmi
Fahmi

Reputation: 37473

you can use join

select c1,c3 from t1 
inner join t2 on t1.c1=t2.c4
where c2='a' 

Upvotes: 1

Related Questions