在WHERE中使用多个值进行更新

时间:2022-06-06 23:11:08

I need to update the field in database when it match the two conditions below is my code
this is the query which is working fine for one value in WHERE condition

我需要更新数据库中的字段,当它符合下面的两个条件是我的代码这是在WHERE条件中正常工作的查询

query2 = "UPDATE  Game_mygame 
          SET game_played = "+str(set_game_played)+" 
          WHERE home_teamID = "+str(i.home_teamID) 

but I need to match two values in WHERE and I am trying the following

但我需要匹配WHERE中的两个值,我正在尝试以下方法

query2 = "UPDATE  Game_mygame 
              SET game_played = "+str(set_game_played)+" 
              WHERE home_teamID = "+str(i.home_teamID)+ 
                    "AND away_teamID = "+str(i.away_teamID)  

but this one gives the syntax error

但是这个给出了语法错误

Please suggest me where I am doing mistake

请建议我在哪里做错了

2 个解决方案

#1


2  

I think there is no space between first WHERE condition and AND. So it will make your query like this WHERE home_teamID=xyzAND instead of WHERE home_teamID=xyz AND. So you need to give an extra space before AND (same as you have given it before WHERE)

我认为第一个WHERE条件和AND之间没有空格。所以它会使你的查询像这样WHERE home_teamID = xyzAND而不是WHERE home_teamID = xyz AND。所以你需要在AND之前给出一个额外的空间(就像你在WHERE之前给出的那样)

query2 = "UPDATE  Game_mygame 
              SET game_played = "+str(set_game_played)+
              " WHERE home_teamID = "+str(i.home_teamID)+ 
              "AND away_teamID = "+str(i.away_teamID)
           ---^^--- Space is missing here

So try this:

所以试试这个:

query2 = "UPDATE  Game_mygame 
              SET game_played = "+str(set_game_played)+
              " WHERE home_teamID = "+str(i.home_teamID)+ 
              " AND away_teamID = "+str(i.away_teamID)

#2


1  

Concat with . like

康卡特。喜欢

query2 = "UPDATE  Game_mygame 
          SET game_played = ".str(set_game_played)." 
          WHERE home_teamID = '". str(i.home_teamID) ."'
          AND away_teamID = '".str(i.away_teamID)."'";  

And also as @Preet Sangha said we can use ids without quotes like

而且正如@Preet Sangha所说,我们可以使用没有引号的ID

query2 = "UPDATE  Game_mygame 
          SET game_played = ".str(set_game_played)." 
          WHERE home_teamID = ". str(i.home_teamID) ."
          AND away_teamID = ".str(i.away_teamID);  

#1


2  

I think there is no space between first WHERE condition and AND. So it will make your query like this WHERE home_teamID=xyzAND instead of WHERE home_teamID=xyz AND. So you need to give an extra space before AND (same as you have given it before WHERE)

我认为第一个WHERE条件和AND之间没有空格。所以它会使你的查询像这样WHERE home_teamID = xyzAND而不是WHERE home_teamID = xyz AND。所以你需要在AND之前给出一个额外的空间(就像你在WHERE之前给出的那样)

query2 = "UPDATE  Game_mygame 
              SET game_played = "+str(set_game_played)+
              " WHERE home_teamID = "+str(i.home_teamID)+ 
              "AND away_teamID = "+str(i.away_teamID)
           ---^^--- Space is missing here

So try this:

所以试试这个:

query2 = "UPDATE  Game_mygame 
              SET game_played = "+str(set_game_played)+
              " WHERE home_teamID = "+str(i.home_teamID)+ 
              " AND away_teamID = "+str(i.away_teamID)

#2


1  

Concat with . like

康卡特。喜欢

query2 = "UPDATE  Game_mygame 
          SET game_played = ".str(set_game_played)." 
          WHERE home_teamID = '". str(i.home_teamID) ."'
          AND away_teamID = '".str(i.away_teamID)."'";  

And also as @Preet Sangha said we can use ids without quotes like

而且正如@Preet Sangha所说,我们可以使用没有引号的ID

query2 = "UPDATE  Game_mygame 
          SET game_played = ".str(set_game_played)." 
          WHERE home_teamID = ". str(i.home_teamID) ."
          AND away_teamID = ".str(i.away_teamID);