小编Jay*_*nex的帖子

Mysql更新以相同名称开头的所有列

我有一个名为'products'的动态表,其中包含多种语言.表格列如下所示:

id, product_id, store_de, store_en, store_fr, store_es... etc
Run Code Online (Sandbox Code Playgroud)

语言可以或多或少.

现在我想更新此表并将以"store_"开头的所有列设置为值1.

我尝试了以下方法:

$stmt = $dbh->prepare( "UPDATE products SET `store_%` = ? WHERE product_id = ?" );

$stmt->execute( array( 1, $lastID ) );
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'store%'

有没有办法更新以'store_'开头的所有列,或者我是否必须列出所有列?


基于jekaby的答案,这里有一个对我有用的完整解决方案:

$get_stores = $dbh->prepare("SHOW COLUMNS FROM products_active WHERE field REGEXP '^store'");
    $get_stores->execute();
    $stores = $get_stores->fetchAll();

    $update_string = "";
    $first_store = true;
    foreach($stores as $store) {
        if(!$first_store) {

            $update_string .= ", ";

        } else {

            $first_store = false;

        }
        $update_string .= $store['Field']." = '".$status."'";

    }

    $update_activity …
Run Code Online (Sandbox Code Playgroud)

php mysql sql-like

2
推荐指数
1
解决办法
665
查看次数

标签 统计

mysql ×1

php ×1

sql-like ×1