简体   繁体   中英

Insert into table only if most recent record from group is different

I have a MySQL table with the following columns:

score_id (int10, auto_increment, primary key); 
game_id (int10); 
away_team_score (int10); 
home_team_score (int10); 
date_time (datetime);

I am scraping a web API (using python) which I am trying to write an array to this database. However, each time I read this API it provides a list of all events. I am trying to write to this database only when there is a difference in either the away_team_score or the home_team_score for each game_id.

I am able to get the most-recent records using the query from this example ( mySQL GROUP, most recent ). However, I am unsure on how to check if the values that I am inserting are the same.

I don't want to use update because I want to keep the scores for historical purposes. Also, if the game_id does not exist, it should insert it.

My python code I currently have:

# Connecting to the mysql database
mydb = mysql.connector.connect(host="examplehost", user="exampleuser", passwd="examplepassword", database="exampledb")
mycursor = mydb.cursor()
# example scores array that I scraped
# It is in the format of game_id, away_team_score, home_team_score, date_time
scores = [[1, 0, 1, '2019-11-30 13:05:00'], [2, 1, 5, '2019-11-30 13:05:00'], [3, 4, 8, '2019-11-30 13:05:00'],
          [4, 6, 1, '2019-11-30 13:05:00'], [5, 0, 2, '2019-11-30 13:05:00']]

# Inserting into database
game_query = "INSERT INTO tbl_scores (game_id, away_team_score, home_team_score, date_time) VALUES (%s, %s, %s, %s)"

mycursor.executemany(game_query, scores)
mydb.commit()

mydb.close()

You need to make use of UPSERT functionality in MySQL. Changing your insert query to the following query will only insert when there is new game-id, else it will update the scores:

INSERT INTO tbl_scores
    (game_id, score_id, away_team_score, home_team_score, date_time)
VALUES
    (game_id, score_id, away_team_score, home_team_score, date_time)
ON DUPLICATE KEY UPDATE
    game_id = game_id,
    away_team_score = away_team_score,
    home_team_score = home_team_score,
    date_time = date_time;

Details on upsert - https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html

Let me know if that helps.

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