簡體   English   中英

如何從一個表中選擇 id 並插入/更新到另一個表

[英]How to select id's from one table and insert/update to another table

我正在使用 python 開發一個自動交易系統,我建立了一個數據庫,每次生成新訂單時都會插入一行。

我遇到的問題是,如果訂單成功,我想將 OrderID 添加到我的頭寸表中。

以下是可能發生的場景

如果沒有具有相同OrderID, AccountID, strategyID, AssetID開放引用OrderID, AccountID, strategyID, AssetID則插入新行,其中PositionID=NUM, OpenReference=OrderID, CloseReference=NULL, Status=2

如果存在不屬於status(3)倉位,請檢查它是否與 OpenReference 參數( OrderID, AccountID, strategyID, AssetID )匹配OrderID, AccountID, strategyID, AssetID如果它確實將OrderID更新為列CloseReference並更新status=3

訂單表設置

CREATE TABLE `__order` (
  `OrderID` int NOT NULL AUTO_INCREMENT,
  `AccountID` int DEFAULT NULL,
  `StrategyID` int DEFAULT NULL,
  `AssetID` int DEFAULT NULL,
  `TimeSubmitted` datetime DEFAULT NULL,
  `Action` mediumtext,
  `Type` mediumtext,
  `Price` float DEFAULT NULL,
  `Quantity` int DEFAULT NULL,
  `Outstanding` int DEFAULT NULL,
  `TimeCompleted` datetime DEFAULT NULL,
  `Commission` float DEFAULT NULL,
  `Status` mediumtext,
  PRIMARY KEY (`OrderID`),
  KEY `AssetID_FORK_idx` (`AssetID`),
  KEY `AccountID_FORK_idx` (`AccountID`),
  KEY `StratID_FORK_idx` (`StrategyID`),
  CONSTRAINT `AccountID_FORK` FOREIGN KEY (`AccountID`) REFERENCES `__account` (`AccountID`),
  CONSTRAINT `AssetID_FORK` FOREIGN KEY (`AssetID`) REFERENCES `__asset` (`AssetID`),
  CONSTRAINT `StratID_FORK` FOREIGN KEY (`StrategyID`) REFERENCES `__strategy` (`StrategyID`)
) ENGINE=InnoDB AUTO_INCREMENT=577 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

位置表設置

CREATE TABLE `__position` (
  `PositionID` int NOT NULL AUTO_INCREMENT,
  `OpenReference` int DEFAULT NULL,
  `CloseReference` int DEFAULT NULL,
  `Status` int DEFAULT NULL,
  PRIMARY KEY (`PositionID`),
  KEY `BuyReference_order_FK_idx` (`OpenReference`),
  KEY `SellReference_order_FK_idx` (`CloseReference`),
  KEY `Status_order_FK_idx` (`Status`),
  CONSTRAINT `BuyReference_order_FK` FOREIGN KEY (`OpenReference`) REFERENCES `__order` (`OrderID`),
  CONSTRAINT `SellReference_order_FK` FOREIGN KEY (`CloseReference`) REFERENCES `__order` (`OrderID`),
  CONSTRAINT `Status_order_FK` FOREIGN KEY (`Status`) REFERENCES `__status` (`StatusID`)
)

我的 Python 代碼

def insert_position(self, openRef=None, status=None):
    return self.execute(
        sql="""
            INSERT INTO __position
                (OpenReference, Status) 
            VALUES 
                (%s, %s);""",
        params=(openRef, status,))

def update_position(self, positionID, closeRef=None, status=None):
    return self.execute(
        sql="""
            UPDATE __position
                SET CloseReference = %s,
                    Status = %s
            WHERE PositionID = %s;""", params=(closeRef, status, positionID,))

SELECT查詢開始,檢查是否有任何具有給定訂單 ID 的頭寸。

如果有,根據需要更新所有status != 3 如果不是,則插入新行。

def insert_or_update_position(self, order_id):
    self.execute(sql = "SELECT 1 FROM __position WHERE OpenReference = %s LIMIT 1", (order_id,))
    row = self.cursor.fetchone()
    if row:
        self.execute(sql="""
            UPDATE __position
            SET CloseReference = %s, Status = 3
            WHERE OpenReference = %s AND Status != 3""", (order_id, order_id))
    else:
        return self.execute(sql="""
            INSERT INTO __position (OpenReference, Status)
            VALUES (%s, 2)""", (order_id,))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM