<i id='xXxxV'><tr id='xXxxV'><dt id='xXxxV'><q id='xXxxV'><span id='xXxxV'><b id='xXxxV'><form id='xXxxV'><ins id='xXxxV'></ins><ul id='xXxxV'></ul><sub id='xXxxV'></sub></form><legend id='xXxxV'></legend><bdo id='xXxxV'><pre id='xXxxV'><center id='xXxxV'></center></pre></bdo></b><th id='xXxxV'></th></span></q></dt></tr></i><div id='xXxxV'><tfoot id='xXxxV'></tfoot><dl id='xXxxV'><fieldset id='xXxxV'></fieldset></dl></div>

<small id='xXxxV'></small><noframes id='xXxxV'>

<tfoot id='xXxxV'></tfoot>

      <bdo id='xXxxV'></bdo><ul id='xXxxV'></ul>
    <legend id='xXxxV'><style id='xXxxV'><dir id='xXxxV'><q id='xXxxV'></q></dir></style></legend>

      1. MySQL“好"如果未找到则插入一行,如果找到则更新它

        MySQL quot;goodquot; way to insert a row if not found, or update it if it is found(MySQL“好如果未找到则插入一行,如果找到则更新它)
        <legend id='Yd1gI'><style id='Yd1gI'><dir id='Yd1gI'><q id='Yd1gI'></q></dir></style></legend>

        1. <i id='Yd1gI'><tr id='Yd1gI'><dt id='Yd1gI'><q id='Yd1gI'><span id='Yd1gI'><b id='Yd1gI'><form id='Yd1gI'><ins id='Yd1gI'></ins><ul id='Yd1gI'></ul><sub id='Yd1gI'></sub></form><legend id='Yd1gI'></legend><bdo id='Yd1gI'><pre id='Yd1gI'><center id='Yd1gI'></center></pre></bdo></b><th id='Yd1gI'></th></span></q></dt></tr></i><div id='Yd1gI'><tfoot id='Yd1gI'></tfoot><dl id='Yd1gI'><fieldset id='Yd1gI'></fieldset></dl></div>
            <tbody id='Yd1gI'></tbody>
          <tfoot id='Yd1gI'></tfoot>
          • <bdo id='Yd1gI'></bdo><ul id='Yd1gI'></ul>

                  <small id='Yd1gI'></small><noframes id='Yd1gI'>

                  本文介绍了MySQL“好"如果未找到则插入一行,如果找到则更新它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  很多时候,我想对我的一个用户运行查询,我希望在其中存储一行并与该用户关联,以 1 对 1 的关系.所以让我们说(这只是一个随意的例子),我有一个表格来跟踪用户的汽车,以及一些关于汽车的信息.每个用户可以拥有 0 辆或 1 辆汽车.如果用户没有汽车,则表中没有该用户的条目.

                  Very often, I want to run a query on one of my users where I want a row stored and associated with that user, in a 1-to-1 relationship. So let's say (this is just an arbitrary example), that I have a table that keeps track of a user's car, along with some info about the car. Each user can have either 0 or 1 cars. If the user has no car, there is no entry in the table for that user.

                  cars 表(同样,只是一个例子):id、user_id、car_make、car_model

                  cars table (again, just an example): id, user_id, car_make, car_model

                  所以,当我更新这个表时,我总是做这样的事情(伪代码):

                  So, when I update this table, I always end up doing something like this (pseudo-code):

                  result = SELECT * FROM cars WHERE user_id=5
                  if (num_rows(result)>0){
                      UPDATE cars SET car_make='toyota', car_model='prius' WHERE user_id=5
                  }else{
                      INSERT INTO cars (user_id, car_make, car_model) VALUES (5, 'toyota', 'prius')
                  }
                  

                  我怎样才能把它变成一个原子地"工作的优雅语句?如果在另一个进程中,行在 SELECT 和 UPDATE 语句之间被 REMOVED 会发生什么?我的 UPDATE 语句将在 INSERT 语句应该运行的地方失败.而且我觉得我需要做两个相似(但不同)的语句来完成同样的事情!我需要的是一些声明,以确保我想要的数据存在于表中,尤其是当我只想要 1 行满足我的要求时.例如,它可能是这样的(当然这完全是编造的):

                  How can I make this into one elegant statement that works "atomically"? What happens if, in another process, the row is REMOVED between the SELECT and UPDATE statements? My UPDATE statement will fail where the INSERT statement should have run. And I feel like I need to do two similar (but different) statements to accomplish the same thing! What I need is some statement that will assure me that the data I want exists in the table, especially when I only want 1 row that satisfies my requirement. For example, it might be something like (this is totally made-up of course):

                  MAKE SURE A ROW IN cars WHERE user_id=5 IS SET WITH car_make='toyota', car_model='prius'
                  

                  这样,如果user_id 5 已经存在,则更新,否则插入.此外,如果我更改了要求,例如说每个用户可以拥有给定 car_make 的零辆或一辆汽车,那么我可以进一步指定:

                  That way, if user_id of 5 exists already, it will be updated, otherwise, it will be inserted. Also, if I changed the requirements, for example to say that each user can have zero or one cars of a given car_make, then I could further specify that:

                  MAKE SURE A ROW IN cars WHERE user_id=5 AND car_make='toyota' IS SET WITH car_model='prius'
                  

                  我希望我的问题有意义!我如何改进这种经常出现的基本插入如果未找到或更新如果找到操作?感谢您的帮助!

                  I hope my question makes sense! How can I improve this basic insert-if-not-found or update-if-found operation that comes up so often? Thanks for any help!

                  推荐答案

                  您可以使用 "REPLACE INTO" 或 "插入...在重复的密钥更新中".我相信第二种是你想要的,但在某些情况下 REPLACE INTO 很方便.

                  You can use "REPLACE INTO" or "INSERT… ON DUPLICATE KEY UPDATE". I believe the second is what you want, but there are situations where REPLACE INTO is convenient.

                  这篇关于MySQL“好"如果未找到则插入一行,如果找到则更新它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Import CSV to Update rows in table(导入 CSV 以更新表中的行)
                  Importing MaxMind#39;s GeoLite2 to MySQL(将 MaxMind 的 GeoLite2 导入 MySQL)
                  How to save a Google maps overlay shape in the database?(如何在数据库中保存谷歌地图叠加形状?)
                  Is there a performance hit using decimal data types (MySQL / Postgres)(使用十进制数据类型(MySQL/Postgres)是否会影响性能)
                  MySQL truncates concatenated result of a GROUP_CONCAT function(MySQL 截断 GROUP_CONCAT 函数的连接结果)
                  What#39;s the difference between VARCHAR(255) and TINYTEXT string types in MySQL?(MySQL 中的 VARCHAR(255) 和 TINYTEXT 字符串类型有什么区别?)
                    <bdo id='VSDBk'></bdo><ul id='VSDBk'></ul>

                  • <legend id='VSDBk'><style id='VSDBk'><dir id='VSDBk'><q id='VSDBk'></q></dir></style></legend>

                        <small id='VSDBk'></small><noframes id='VSDBk'>

                          <tbody id='VSDBk'></tbody>
                        • <i id='VSDBk'><tr id='VSDBk'><dt id='VSDBk'><q id='VSDBk'><span id='VSDBk'><b id='VSDBk'><form id='VSDBk'><ins id='VSDBk'></ins><ul id='VSDBk'></ul><sub id='VSDBk'></sub></form><legend id='VSDBk'></legend><bdo id='VSDBk'><pre id='VSDBk'><center id='VSDBk'></center></pre></bdo></b><th id='VSDBk'></th></span></q></dt></tr></i><div id='VSDBk'><tfoot id='VSDBk'></tfoot><dl id='VSDBk'><fieldset id='VSDBk'></fieldset></dl></div>
                            <tfoot id='VSDBk'></tfoot>