Home  >  Q&A  >  body text

Insert data if and only if there is no duplicate data in the mysql database

<insert id="insertStockOrderAbusingList" parameterType="java.util.List">
    INSERT IGNORE INTO iruda_trade.stock_order_abusing (
        market_day,
        uid,
        stock_account_id,
        order_number,
        symbol,
        conclusion_quantity,
        order_type,
        created_at
    ) VALUES
    <foreach collection="list" item="item" index="index" separator=",">
    (
            #{item.transactionDate},
            #{item.uid},
            #{item.stockAccountId},
            #{item.transactionNumber},
            #{item.symbol},
            #{item.filledVolume},
            #{item.transactionType},
            #{item.clientTimeStamp}
     )
     </foreach>
</insert>

I'm trying to insert data into mySQL DB. If the transactionDate, transactionNumber and uid in the parameters are also the same as the market_day, order_number, uid in the stock_order_abusing table column, then there will be no insertion or the insertion should be ignored. There is no pk (primary key) to compare. All methods in Google tell me that there should be primary key to prevent duplicate insertion. Is there any way to solve this problem?

P粉883278265P粉883278265402 days ago466

reply all(1)I'll reply

  • P粉216807924

    P粉2168079242023-09-14 13:57:28

    If you want to take no action when duplicate records exist, you can use the INSERT IGNORE syntax.

    Here is the complete grammar guide

    reply
    0
  • Cancelreply