Q:

howto have condition in a nested SQL query?

here is my SQL statement , i would like to find all the games that have the status 0 and names of teams that are like key_word or the sport's name that are like the key word.
The problem is that all the games that are displayed don't have status 0 .
What am i doing wrong?
sql="select * from games where games.status=0 and games.team_2_id
     IN (select id from teams where name like '"+key_word+"')
      or games.team_1_id
        IN (select id from teams where name like '"+key_word+"')
          or games.sport like '"+key_word+"'
            "

A:

It looks like you simply need to enclose your OR conditions separately in parenthesis:
SELECT * 
FROM   games 
WHERE  games.status = 0 AND 
       (games.team_2_id IN (SELECT id FROM teams WHERE name LIKE '%key_word%') OR
        games.team_1_id IN (SELECT id FROM teams WHERE name LIKE '%key_word%') OR
        games.sport LIKE '%key_word%')

