简体   繁体   English

如果更新多行时该行尚不存在,该如何插入?

[英]How can I insert a row if it doesn't already exist while updating multiple rows?

I have a MySQL query that looks like this: 我有一个如下所示的MySQL查询:

UPDATE `Table` SET `Column` = 
    CASE 
      WHEN `Option Id` = '1' THEN 'Apple'
      WHEN `Option Id` = '2' THEN 'Banana'
      WHEN `Option Id` = '3' THEN 'Q-Tip'
    END 

An my table currently looks like this: 我的表格目前看起来像这样:

Option Id | Column
        1 |      x
        2 |      x

I'd like it to result in: 我希望它导致:

Option Id | Column
        1 |  Apple
        2 | Banana
        3 |  Q-Tip

But it doesn't insert the Q-Tip row. 但是它不会插入Q-Tip行。 I've looked up and read a bit about INSERT ON DUPLICATE KEY UPDATE and REPLACE , but I can't find a way to get those to work with this multiple row update using CASE . 我查找并阅读了一些有关INSERT ON DUPLICATE KEY UPDATEREPLACE ,但是我找不到一种使用CASE使其与多行更新一起工作的方法。 Do I have to write a separate query for each row to get this to work, or is there a nice way to do this in MySQL? 我是否必须为每一行编写一个单独的查询才能使它工作,还是在MySQL中有一种不错的方法呢?

Option Id is not a Key itself, but it is part of the Primary Key . Option Id本身不是Key ,但它是Primary Key一部分。

EDIT Some more info: 编辑更多信息:

I'm programming in PHP, and essentially I'm storing an array for the user. 我使用PHP进行编程,并且实质上是为用户存储一个数组。 Option Id is the key, and Column is the value. Option Id是键, Column是值。 So for simplicities sake, my table could look like: 因此,为简单起见,我的表可能如下所示:

User Id | Option Id | Value
     10 |         1 | Apple
     10 |         2 |  Shoe
     11 |         1 |  Czar
...

That user can easily update the elements in the array and add new ones, then POST the array to the server, in which case I'd like to store it in the table. 用户可以轻松更新数组中的元素,并添加新的,然后POST数组服务器,我想在这种情况下,将其存储在表中。 My query above updates any array elements that they've edited, but it doesn't insert the new ones. 我上面的查询更新了所有已编辑的数组元素,但没有插入新的数组元素。 I'm wondering if there is a query that can take my array from POST and insert it into the table without me having to write a loop and have a query for every array element. 我想知道是否有一个查询可以从POST获取我的数组并将其插入表中,而无需我编写循环并为每个数组元素查询。

Of course it does not insert. 当然不会插入。 As there is no such value, it cannot get updated. 由于没有这样的值,因此无法更新。

I suppose you are normalizing a database by putting in the values already present and now want to add the required mapping for every valid value. 我想您正在通过放入已经存在的值来规范化数据库,现在想为每个有效值添加所需的映射。

So it would be better to start from scratch and just do INSERTs. 因此,最好从头开始,只做INSERT。

This should work, if Option_Id is a primary key: 如果Option_Id是主键,这应该可以工作:

REPLACE INTO `Table` (`Option_Id`, `Column`) VALUES
(1, 'Apple'),
(2, 'Banana'),
(3, 'Q-Tip');

The statement means: Insert the given rows or replace the values, if the PK is already existing. 该语句的意思是:如果PK已经存在,则插入给定的行或替换值。

您总是可以查询数据库中的条目,然后根据您的结果选择更新或插入

I see no point in such updating. 我认为这种更新毫无意义。
Why don't you have a separate table with option ids and corresponding values, leaving only option ids linked to user ids in this one? 为什么您没有一个单独的表,其中包含选项ID和相应的值,而在此表中仅保留链接到用户ID的选项ID?

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

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