WHERE clause return incorrect result

时间:2018-06-04 17:44:14

标签: mysql sql

I want return all the records that have as team_id a certain number and have a specific round_id, so I wrote this query:

SELECT * FROM `match` WHERE home_team_id = 68 OR away_team_id = 68 AND round_id = 70

this will return:

{
    "id": "61032",
    "round_id": "70",
    "home_team_id": "68",
    "away_team_id": "76",
},
{
    "id": "61052",
    "round_id": "75",
    "home_team_id": "68",
    "away_team_id": "74",
},

why I get also the record with round_id = 75?

2 个答案:

答案 0 :(得分:0)

You probably just need some parentheses to group your logic statements:

SELECT * 
FROM match 
WHERE home_team_id = 68 
    OR (
        away_team_id = 68 
        AND 
        round_id = 70
        )

答案 1 :(得分:0)

我认为你想要的逻辑是:

where 68 in (home_team_id, away_team_id) and round_id = 70

您还可以通过调整括号来修复逻辑。