开发者

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 wher开发者_如何学运维e name like '"+key_word+"')
          or games.sport like '"+key_word+"'
            "


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%')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜