Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

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

like image 982
M. of CA Avatar asked Aug 09 '11 23:08

M. of CA


2 Answers

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.

like image 196
tkrajcar Avatar answered Oct 13 '22 22:10

tkrajcar


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.

like image 35
Dan Grossman Avatar answered Oct 13 '22 22:10

Dan Grossman