如果 table 的列等于第二个 table 中的列,则在第三个 table 中插入值,python - mysql

If a table has columns equal to columns in a second table insert values in a third table, python - mysql

我的数据库中有 3 个 table:

我在 python 中使用输入命令创建了一个循环,我可以在其中插入新的贷款请求和新的出价。 每个插入都与 mysql 连接,并添加到相应的 table.

例如:

input in python --> L 1000 1.1 output --> 在 Borrowers table 中创建插入 auto_incremental PK,数量为 1000利率为 1.1%

我需要在循环中创建另一个函数,其中对于每个新的 request/offer,如果报价等于请求(Borrowers.Amount, BorrowersInterestRate = Lenders.Amount, Lenders.InterestRate ),它将以相同的金额和利率在合同 table 中创建插入。

这仅在您使用 mysql python 连接器时有效。据我所知,这是使用 python 使用 mysql 的唯一方法,但我可能错了。

搜索请求是否与报价匹配:

def search_for_request(amount, interest_rate):
    # assuming that columns containing amount and interest rate are named as such
    database.cursor.execute("select amount_of_the_bid, interest_rate from lenders")
    result = database.cursor.fetchall()
    for amount_and_interest in result:
        # not sure if this step is necessary
        amount_and_interest = list(amount_and_interest)
        if amount_and_interst[0] == amount and amount_and_interst[1] == interest_rate:
            # execute query for entering the contract info in contract table
            database.commit()

搜索报价是否符合要求:

def search_for_offer(amount, interest_rate):
    # assuming that columns containing amount and interest rate are named as such
    database.cursor.execute("select amount_of_the_bid, interest_rate from borrowers")
    result = database.cursor.fetchall()
    for amount_and_interest in result:
        # not sure if this step is necessary
        amount_and_interest = list(amount_and_interest)
        if amount_and_interest[0] == amount and amount_and_interest[1] == interest_rate:
            # execute query for entering the contract info in contract table
            database.commit()