Page 1 of 1

select data from a table and it satisfy two conditions

Posted: Mon Apr 13, 2009 1:47 am
by nmreddy
i have a table of data like this with four columns(id1,id2,keyname,keyvalue)

id1 id2 keyname keyvalue

1 -- 111 -- a -- 2

2 -- 111 -- b -- 3

3 -- 222 -- a -- 2

4 -- 222 -- b -- 4


how can i get the id2 value = 111

i written this query

select id2 from table where (a=2) and (b=3)

but it giving empty result.

can any one suggest me the better solution ?

when i placed (or) in between the conditions it giving result but i need to satisfy both conditions

Re: select data from a table and it satisfy two conditions

Posted: Mon Apr 13, 2009 6:15 pm
by tech603
select id2 from table where (a=2) and (b=3)
That would do it, but then if you had any other rows that had 111 in it then you would never get the proper results. From looking at your table set up

Code: Select all

id1 id2 keyname keyvalue
 
1 -- 111 -- a -- 2
 
2 -- 111 -- b -- 3
 
3 -- 222 -- a -- 2
 
4 -- 222 -- b -- 4
 
 
/* this would return all rows that had that value */
select * from table where id2 = 111
 
Hope that helps.

Re: select data from a table and it satisfy two conditions

Posted: Wed Apr 15, 2009 3:22 am
by nmreddy
yes, i have other rows with id2 as 111 like

id1 id2 keyname keyvalue

1 -- 111 -- a -- 2

2 -- 111 -- b -- 3

3 -- 222 -- a -- 2

4 -- 222 -- b -- 4

5 -- 111-- d -- 6


Here i have to get id2 value which is 111 ,with the given conditions of (a=2) and (b=3)

how can i write the query ?

Thanks,
NMreddy

Re: select data from a table and it satisfy two conditions

Posted: Wed Apr 15, 2009 4:54 am
by susrisha

Code: Select all

 
 
select * from tbl where id2 =111 AND ((keyname ='a' and keyvalue='2') OR (keyname='b' and keyvalue='3'));
 

Re: select data from a table and it satisfy two conditions

Posted: Fri Apr 17, 2009 1:03 am
by nmreddy
here i have to get id2 value and it satisfy both conditions ((keyname ='a' and keyvalue='2') AND (keyname='b' and keyvalue='3'))