简体   繁体   English

在MySQL的多列中替换相同字符

[英]Replace same character in multiple columns in MySQL

I want to run this basic replace on columns field01 through field39 without requiring 39 queries: 我想在field01到field39上运行此基本替换,而无需39个查询:

UPDATE leads
    SET field01 = REPLACE(field01, ' ', '+')
     WHERE field40 = 'in_process';

If it is easier to run with PHP, that would be fine. 如果使用PHP更容易运行,那就很好了。

You can set multiple columns within a single query: 您可以在一个查询中设置多个列:

UPDATE leads
    SET field01 = REPLACE(field01, ' ', '+'),
        field02 = REPLACE(field02, ' ', '+'),
        . . .
        field39 = REPLACE(field39, ' ', '+')
    WHERE field40 = 'in_process';

Typing out the code is cumbersome, You can generate the code using a spreadsheet. 输入代码很麻烦,您可以使用电子表格生成代码。

I should not that having dozens of columns that contain similar information is often a sign of bad database design. 我不应该认为包含数十个包含相似信息的列通常是数据库设计不良的标志。 There is a good chance that you need a junction table, with one row per lead and field value. 您很有可能需要一个联结表,每个引线和字段值一行。

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

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