How to retrive value by matching and also not matching

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

How to retrive value by matching and also not matching

Post by shivam0101 »

Hello,

i have two fields, f1 and f2

Code: Select all

SELECT * FROM table WHERE f1=96 OR f2=96

96 is the id.  how do i get rest of the rest of the ids.
I can get their values and filter it out. But, i wanted it done in the query itself.

Thanx
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Your question isn't making sense. What are you after?
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post by shivam0101 »

Clients belong to 2 type of categories, 1- seeker, 2-responder. when a person seeks his id will be entered in f1 and the person's id whom he wants to respond will be entered in f2. So 96 (example id) will be present as a seeker in some row and in responder in some rows. If i wanted to know whom all i have contacted (seeker or responder), how do i write the query?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

So, if I understand correctly, you want to search for all the rows with 96 in either f1 or f2, and return the other id, eg f2 for rows where f1 = 96 or f1 for rows where f2 = 96?

Code: Select all

SELECT
IF (f1=96,f2,f1) as other_id
FROM `table`
WHERE f1 = 96 or f2 = 96
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post by shivam0101 »

onion2k,

Thanx. Its working fine.
Post Reply