简体   繁体   English

是否有可能在mysql中更新然后INSERT?

[英]Is it possible to UPDATE then INSERT in mysql?

Is it possible to UPDATE and then INSERT where row exists in mysql? 是否可以先更新,然后在mysql中存在行的地方进行插入? I have this query, 我有这个问题,

$q = $dbc -> prepare("UPDATE accounts SET lifeforce = maxLifeforce, inHospital = 0 WHERE hospitalTime <= NOW() AND inHospital = 1");
$q -> execute();

How can I either get the primary key into an associative array to then do an insert for each item in the array, or do an UPDATE AND INSERT? 如何将主键转换为关联数组,然后对数组中的每个项执行插入,或者执行UPDATE AND INSERT?

Or does it involve doing a SELECT to get all that match criteria, then UPDATE then INSERT using array from the select? 还是涉及进行SELECT以获得所有匹配条件,然后进行更新,然后使用选择中的数组进行INSERT? This seems rather a long way to do it? 这似乎还有很长的路要走?

Basically I need to INSERT onto another table using the same primary keys that get updated. 基本上,我需要使用要更新的相同主键将其插入到另一个表中。

Or does it involve doing a SELECT to get all that match criteria, then UPDATE then INSERT using array from the select? 还是涉及进行SELECT以获得所有匹配条件,然后进行更新,然后使用选择中的数组进行INSERT?

Yes, sorry, that's the main way. 是的,抱歉,这是主要方式。

Another approach is to add a column called (say) last_updated , that you set whenever you update the column. 另一种方法是添加一个名为(say) last_updated ,您可以在更新列时设置该列。 You can then use that column in a query that drives your insert. 然后,您可以在驱动插入的查询中使用该列。 That would have other advantages — I find last_updated columns to be useful for many things — but it's overkill if this is the only thing you'd ever use it for. 那会带来其他好处-我发现last_updated列对很多事情都有用-但是,如果这是您唯一使用过的东西,那就太过分了。


Edited to add: Another option, which just occurred to me, is to add a trigger to your accounts table, that will perform the insert you need. 编辑添加:我刚刚想到的另一种选择是向您的accounts添加触发器该触发器将执行您需要的插入。 That's qualitatively different — it causes the insertion to be a property of accounts , rather than a matter of application logic — but maybe that's what you want? 这在质量上有所不同-导致插入是accounts的属性,而不是应用程序逻辑的问题-但这也许就是您想要的? Even the most extreme partisans of the "put-all-constraints-in-the-database-so-application-logic-never-introduces-inconsistency" camp are usually cautious about triggers — they're really not a good way to implement application logic, because it hides that logic somewhere that no-one will think to look for it. 即使是“在数据库中放置所有约束 - 所以应用程序逻辑 - 从不引入 - 不一致”阵营的最极端的支持者通常对触发器持谨慎态度 - 它们实际上不是实现应用程序的好方法逻辑,因为它隐藏了那个没有人会想到的逻辑。 But if the table you're inserting into is some sort of account_history table that will keep track of all changes to account , then it might be the way to go. 但是,如果您要插入的表是某种account_history表,该表可以跟踪对account的所有更改,则可能是要采用的方法。

You can use a multiple table update as written in the manual: http://dev.mysql.com/doc/refman/5.0/en/update.html 您可以使用手册中编写的多表更新: http//dev.mysql.com/doc/refman/5.0/en/update.html

If the second table needs an insert, you probably would have to do it manually. 如果第二张表需要插入,则可能必须手动执行。

You can use the mysqli_last_id function: http://php.net/manual/en/mysqli.insert-id.php 您可以使用mysqli_last_id函数: http ://php.net/manual/en/mysqli.insert-id.php

Also, when running consecutive queries like that, I'd recommend using transactions: http://www.techrepublic.com/article/implement-mysql-based-transactions-with-a-new-set-of-php-extensions/6085922 另外,当运行类似的连续查询时,我建议使用事务: http : //www.techrepublic.com/article/implement-mysql-based-transactions-with-a-new-set-of-php-extensions/ 6085922

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

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