简体   繁体   English

在MySQL中结合插入,更新和选择

[英]Combine Insert, Update and Select in MySQL

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? 我怎样才能做到这一点?

存储过程是答案和IF / ELSE

No, it's not possible, in the context of a single SQL statement. 不,在单个SQL语句的上下文中是不可能的。

It is possible to combine and INSERT and UPDATE action into a single statement. 可以将INSERT和UPDATE动作组合到单个语句中。 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. INSERT ... ON DUPLICATE KEY UPDATE语句基本上将尝试插入一行,并且如果该插入导致“重复键”异常,则将执行UPDATE操作。

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. 但是就用SELECT语句测试某些一般条件,然后根据查询结果运行INSERT或UPDATE而言,答案是否定的,MySQL没有任何单个语句语法来支持该条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM