Python,将记录从JOIN语句插入数据库

时间:2018-11-13 22:12:25

标签: python sql database join

我正在尝试将组合表数据写入正在使用的计时系统的新表中。

以下SQL在PHPMyAdmin中有效。

INSERT
INTO
    results(
        firstName,
        lastName,
        raceNumber,
        raceTime
    )
SELECT
    s.firstName,
    s.lastName,
    s.raceNumber,
    h.time
FROM
    runners s
INNER JOIN
    chipData hp
ON
    s.raceNumber = hp.bandID
INNER JOIN
    readings h
ON
    hp.tagId = h.tagId
WHERE
    hp.tagId = 123456
LIMIT 1

但是,如果我按如下所示将其添加到Python语句中,则它将不起作用:

db = connect()
    cur = db.cursor()
    cur.execute("""INSERT INTO results( firstName, lastName, raceNumber, raceTime ) SELECT s.firstName, s.lastName, s.raceNumber, h.time FROM runners s INNER JOIN chipData hp ON s.raceNumber = hp.bandID INNER JOIN readings h ON hp.tagId = h.tagId WHERE hp.tagId = %s LIMIT 1""", (123456)
    db.commit()
    db.close()

感谢您的帮助!

0 个答案:

没有答案