Is there MySQL.. INSERT … ON DUPLICATE KEY SELECT?

后端 未结 2 1363
刺人心
刺人心 2021-01-18 12:14

is there a way to do an INSERT but on DUPLICATE KEY instead of an update do a SELECT?

相关标签:
2条回答
  • 2021-01-18 12:32

    Not in one query, but you coul run INSERT IGNORE <xxx> and then SELECT <xxx>. The IGNORE makes it ignore any rows that would trigger duplicate key errors instead of halting.

    0 讨论(0)
  • 2021-01-18 12:39

    No. You will have to watch for the duplicate key error then issue your SELECT query.

    It'd be quite problematic if that existed since INSERT and UPDATE queries are meant to modify data and SELECT to return a result set. All kinds of drivers/interfaces handle these cases differently for good reason.

    0 讨论(0)
提交回复
热议问题