简体   繁体   English

MySQL中更新查询中的多集和where子句

[英]Multiple set and where clauses in Update query in mysql

I don't think this is possible as I couldn't find anything but I thought I would check on here in case I am not searching for the correct thing. 我认为这是不可能的,因为我找不到任何东西,但是我认为如果我没有搜索正确的东西,我会在这里进行检查。

I have a settings table in my database which has two columns. 我的数据库中有一个设置表,该表有两列。 The first column is the setting name and the second column is the value. 第一列是设置名称,第二列是值。

I need to update all of these at the same time. 我需要同时更新所有这些。 I wanted to see if there was a way to update these values at the same time one query like the following 我想看看是否有一种方法可以同时更新一个查询,如下所示

UPDATE table SET col1='setting name' WHERE col2='1 value' AND SET col1='another name' WHERE col2='another value';

I know the above isn't a correct SQL format but this is the sort of thing that I would like to do so was wondering if there was another way that this can be done instead of having to perform separate SQL queries for each setting I want to update. 我知道上述方法不是正确的SQL格式,但这是我想做的事情,我想知道是否还有另一种方法可以完成此操作,而不必为我想要的每个设置执行单独的SQL查询更新。

Thanks for your help. 谢谢你的帮助。

You can use INSERT INTO .. ON DUPLICATE KEY UPDATE to update multiple rows with different values. 您可以使用INSERT INTO .. ON DUPLICATE KEY UPDATE来更新具有不同值的多行。

You do need a unique index (like a primary key) to make the "duplicate key"-part work 您确实需要唯一索引(例如主键)才能使“重复键”部分起作用

Example: 例:

INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
  ON DUPLICATE KEY UPDATE b = VALUES(b), c = VALUES(c);

-- VALUES(x) points back to the value you gave for field x
-- so for b it is 2 and 5, for c it is 3 and 6 for rows 1 and 4 respectively (if you assume that a is your unique key field)

If you have a specific case I can give you the exact query. 如果您有特殊情况,我可以给您确切的查询。

  UPDATE table
    SET col2 = 
        CASE col1 
           WHEN 'setting1' 
           THEN 'value' 
           ELSE col2 
        END
   , SET col1 = ...
    ...

I decided to use multiple queries all in one go. 我决定一次使用多个查询。 so the code would go like 所以代码会像

UPDATE table SET col2='value1' WHERE col1='setting1';
UPDATE table SET col2='value2' WHERE col1='setting1';

etc etc

I've just done a test where I insert 1500 records into the database. 我刚刚做了一个测试,将1500条记录插入数据库。 Do it without starting a DB transaction and it took 35 seconds, blanked the database and did it again but starting a transaction first, then once the 1500th record inserted finish the transaction and the time it took was 1 second, so definetely seems like doing it in a db transaction is the way to go. 在不启动数据库事务的情况下执行此操作,该过程花费了35秒,清空数据库并再次执行,但是首先启动了一个事务,然后一旦插入第1500条记录完成了事务,并且花费了1秒钟的时间,因此看起来确实很喜欢在数据库事务中是要走的路。

如果要以原子方式运行,则需要运行单独的SQL查询并使用事务。

UPDATE table SET col1=if(col2='1 value','setting name','another name') WHERE col2='1 value' OR col2='another value'

@Frits Van Campen, @Frits Van Campen,

The insert into .. on duplicate works for me. 对重复的..插入对我有用。 I am doing this for years when I want to update more than thousand records from an excel import. 当我想从excel导入中更新数千条记录时,我已经做了很多年了。

Only problem with this trick is, when there is no record to update, instead of ignoring, this method inserts a record and on some instances it is a problem. 此技巧的唯一问题是,当没有记录要更新时,此方法将插入一条记录,而不是忽略它,并且在某些情况下会出现问题。 Then I need to insert another field, then after import I have to delete all the records that has been inserted instead of update. 然后,我需要插入另一个字段,然后在导入后,我必须删除已插入的所有记录,而不是更新。

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

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