简体   繁体   中英

Update column B to column A, but only If table A is empty

I need to move the values in the column name

options_2

into the column name

options_1

But only on the condition when options_1 is empty . (Point A in the image.) If options_1 is not empty ( Point B in the image ), then nothing should happen as I do not want to overwrite the existing values.

Additionally, I would like to then clear our options_2. But this time the rule should be to only clear our options_2 if it is the exact duplicate of options_1. This would make sure that it won't delete all the option_2 values, which are valid ones further down in the database.

在此处输入图片说明

The structure is as follows:

database_name >> wp_cart66_products

I researched and found the command:

update 'wp_cart66_products' set 'options_1' = 'options_2'

But this has no conditional rules, and would transfer and overwrite valid values as well, which I do not want.

update wp_cart66_products
set options_1 = options_2,
    options_2 = ''
where options_1 IS NULL or options_1 = ''

This should do what you need. Note that is goes ahead and clears options_2 when copying the data over, as in this case options 1 & 2 would have been equal if option 2 remained unchanged.

UPDATE `wp_cart66_products`
SET
    `options_1` = `options_2`,
    `options_2` = ''
WHERE
    `options_1` = ''
    OR `options_1` IS NULL

This takes care of all your cases in Block A of your picture, but would not touch those items in Block B. This sounds like what you wanted.

You could then run a second query like this to take care of the cases where option_1 and option_2 match from the start, and you want to clear options_2.

UPDATE `wp_cart66_products`
SET `options_2` = ''
WHERE `options_1` = `options_2`

After your run these two queries you will only have two different cases for your field tuple:

  • options_1 is non-zero-length string and options_2 is empty string
  • options_1 and options_2 are non-matching strings of non-zero length.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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