Using MySQL Workbench to update multiple values in the same columns

笑着哭i 提交于 2019-12-12 01:15:50

问题


I need to run a mysql query to update a stratodesk database.

What I'm trying to achieve: I need to update a whole bunch of thin client PC's through stratodesk, unfortunately there is no bulk update to do this so i am working directly with the MySQL database. The goal is to update the image used and the update mode.

I've got two MySQL queries to do this, one of which will update the image itself the other will update the "image update mode".

if i run the first query it will update the image but if then run the second query to update the image update mode it will undo the first query. My plan was to combine both queries together and update both values at once.

I've done a bit of googling and a lot of the examples go something like this (I'm aware this example does not reflect the database structure I'm working with).

UPDATE orders SET 
    listPrice = 0, 
    bloggerPrice = 0.00, 
    customerPrice = 0.00 
WHERE 
    orders.id = 245745

From This stack overflow topic

Now, this gives me the impression that each of the values there are stored as an individual colomn in the table "orders". I'm pretty certain this would work if the database for stratodesk followed a similar pattern but it doesnt.

They store all this in a table called "CONFIGVALUE" - This has two columns. "CODE" and "VAL" which is where the values i want to update are.

I need to update a) The image

SET CONFIGVALUE.CODE = 'IMAGE',CONFIGVALUE.VAL="2.40.4130d-EEs-k305-181109"

b) the announce mode

SET CONFIGVALUE.CODE = "IMAGE_UPDATE_MODE", CONFIGVALUE.VAL=2

As i say, if i run each then it does update the database. But, the second query appears to undo the first query.

I've combined them together thinking it would solve my issue, but it does not. If i wrap the value for "image" in single quotes, i get an error about truncating incorrect doubles. If i use double quotes, the query will update the "image_update_mode" but does nothing to the "IMAGE" value.

Combined query:

UPDATE IGNORE CONFIGVALUE JOIN CONFIGOBJECT on CONFIGOBJECT.COID=CONFIGVALUE.COID JOIN CLIENTSTATE on CONFIGOBJECT.COID=CLIENTSTATE.COID  JOIN STATUSVALUE on CLIENTSTATE.EID=STATUSVALUE.EID SET CONFIGVALUE.CODE = 'IMAGE', CONFIGVALUE.VAL='2.40.4130d-EEs-k305-181109', CONFIGVALUE.CODE = 'IMAGE_UPDATE_MODE', CONFIGVALUE.VAL=2 WHERE CONFIGOBJECT.PARENT=1221 AND CONFIGOBJECT.COTYPE=3 /*AND STATUSVALUE.VAL="HP t510 Thin Client"*/ AND CONFIGOBJECT.COID=895;

StratoDesk database layout

I've pretty much convinced myself a lot of the issue is coming from the fact the values are both stored in the same columns. In my mind, these values should have their own columns (possibly their own tables).

A secondary question, based on the StratoDesk database layout does it look like there is any kind of normalisation? I'm stil trying to get my head around that. I feel it would be much easier to do if they were, or at least the examples I've seen would make more sense.

Does anybody know how i can achieve this? Or has anybody tried anything like this in the past? If anyone has, does the query look right or is there a cleaner way to do this? I've got a few thousand thin clients to update so would like to get this 100% working before i start updating all the thin clients!

Regards,

Philb


回答1:


If you want to update a column in a row where the values of another row quals a given value you should use a WHERE clause.

To update the image:

UPDATE configvalue
       SET val = '2.40.4130d-EEs-k305-181109'
       WHERE code = 'IMAGE';

To update the image update mode:

UPDATE configvalue
       SET val = '2'
       WHERE code = 'IMAGE_UPDATE_MODE';

Without a WHERE you change the val in all rows.

If you desperately want to have it as one statement you can use a CASE expression that returns the new value if the code matches the code the value is for and otherwise just the old value.

UPDATE configvalue
       SET val = CASE code
                   WHEN 'IMAGE' THEN
                     '2.40.4130d-EEs-k305-181109'
                   WHEN 'IMAGE_UPDATE_MODE' THEN
                     '2'
                   ELSE
                     val
                 END
       WHERE code IN ('IMAGE',
                      'IMAGE_UPDATE_MODE');


来源:https://stackoverflow.com/questions/54025528/using-mysql-workbench-to-update-multiple-values-in-the-same-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!