简体   繁体   English

将B列更新为A列,但仅当表A为空时

[英]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 options_2

into the column name 进入列名

options_1 options_1

But only on the condition when options_1 is empty . 在options_1为的情况下。 (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. (图像中的A点。)如果options_1不为空(图像中的B点),则什么也不会发生,因为我不想覆盖现有值。

Additionally, I would like to then clear our options_2. 此外,我想清除我们的options_2。 But this time the rule should be to only clear our options_2 if it is the exact duplicate of options_1. 但是这次的规则应该是仅清除我们的options_2,如果它与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. 这样可以确保它不会删除所有option_2值,这些值在数据库中更远的地方都是有效的。

在此处输入图片说明

The structure is as follows: 结构如下:

database_name >> wp_cart66_products 数据库名称>> wp_cart66_products

I researched and found the command: 我研究并找到了命令:

update 'wp_cart66_products' set 'options_1' = 'options_2' 更新'wp_cart66_products'设置'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. 请注意,此操作将继续进行,并且在复制数据时会清除options_2 ,因为在这种情况下,如果选项2保持不变,则选项1和2将相等。

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. 这将处理您图片A块中的所有案件,但不会碰到B块中的那些项目。这听起来像您想要的。

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. 然后,您可以运行第二个这样的查询,以解决option_1和option_2从一开始就匹配并且要清除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是非零长度字符串,而options_2是空字符串
  • options_1 and options_2 are non-matching strings of non-zero length. options_1options_2是长度不为零的不匹配字符串。

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

相关问题 如果只有值为空或NULL,则更新MySQL表中的列 - Update a column in MySQL table if only the values are empty or NULL 从表B到表A的MySQL更新列 - MySQL update column from table B to table A 使用值B作为列名更新表A的信息B - Update table A with info of B with value as a column name 如果上一列为空,则仅更新一列(下一列) - Update only one column (next column) if previous column is empty php mysql只更新空列 - Php mysql to update only empty column MySQL 仅在值不为空时更新列 where - MySQL update column only if value not empty where 如果'table a'。'column b'与'table b'。'column a'相匹配,如何用'table b'。'column b'替换'table a'。'column b' - how to replace 'table a'.'column b' with 'table b'.'column b' if 'table a'.'column b' matches 'table b'.'column a' 如果'表a'。'列a'与'表b'。'列a'匹配,如何用'表b'。'列b'替换'表a'。'列b' - how to replace 'table a'.'column b' with 'table b'.'column b' if 'table a'.'column a' matches 'table b'.'column a' Mysql组仅在选择列表A匹配表B时才连接 - Mysql group concat only if selected column table A match table B 仅将 Table_1A 中的 column_A 和 column_B 连接到 Table_2A 的 column_C - join column_A and column_B in Table_1A to Table_2A column_C only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM