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

问题描述

我的数据库中有 3 个表:

  • 借款人:贷款金额、利率、借款人 ID (PK)
  • 贷款人:出价金额、利率和贷款人 ID (PK)
  • Contracts:ContractID PK、金额、利率和两个外键(borrowerID、lenderID)

我在 python 中创建了一个带有输入命令的循环,我可以在其中插入新的贷款请求和新的投标报价。 每次插入都与MysqL相连,并添加到各自的表中。

例如:

python 中的输入 --> L 1000 1.1 输出 --> 在 Borrowers 表中创建一个插入,使用 auto_incremental PK,金额为 1000,利率为 1.1%

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

解决方法

这仅在您使用 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 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()