简体   繁体   中英

Add condition in where clause

I have 3 tables advert , application and people.

 Select * from advert v, application a 
 inner join people p on p.id = a.id 
 where v.id=a.id

This query returns me all application irrespective of the gender. But sometimes in the advert table, gender is specified as M. So now i want the query above return me only application made by M. To get this value i need to add one more condition, p.gender = v.gender . How do i do this? Sometimes the value of v.gender = n/a . Then I wont need this condition. It should return me all application irrespective gender.

To get your desired results, you need to modify the join condition between advert and people to join the records in either case ( v.gender = 'n/a' or p.gender = v.gender ):

select *
  from advert v
  join application a
    on a.id = v.id
  join people p
    on p.id = a.id
   and (v.gender = 'n/a' or p.gender = v.gender)

I'm a little confused as to what you are asking but here is something to start with. Some example data would also be helpful

select *
from advert v
inner join application a
on a.id  = v.id
inner join people p
on a.id = p.id
where p.gender = 'M';

or

 select *
from advert v
inner join application a
on a.id  = v.id
inner join people p
on a.id = p.id
where p.gender = v.gender;

You could potentially use an IN operator thusly:

WHERE v.GENDER IN ('N/A',p.GENDER)

This would return all records where v.GENDER = 'N/A' or where v.gender = p.GENDER

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM