0

Is there a way to combine an insert, update, and select query into one?

Kind of like in a condition, if select query, when the condition is true it will go to update and if not it will go to insert.

How can i do this?

hearmeroar
  • 297
  • 1
  • 8
  • 18

2 Answers2

0

STORED PROCEDURE is the ANSWER and IF/ELSE

jaczes
  • 1,366
  • 2
  • 8
  • 16
0

No, it's not possible, in the context of a single SQL statement.

It is possible to combine and INSERT and UPDATE action into a single statement. The INSERT ... ON DUPLICATE KEY UPDATE statement will basically attempt to INSERT a row, and if that insert results in a "duplicate key" exception, the UPDATE action will be performed instead.

But in terms of a testing some general condition with a SELECT statement, and then running an INSERT or UPDATE depending on the result of the query, the answer is no, MySQL does not have any single statement syntax to support that.

spencer7593
  • 106,611
  • 15
  • 112
  • 140