简体   繁体   English

设置a1在a和b1在b和c1在c等

[英]SET a1 WHERE a AND b1 WHERE b AND c1 WHERE c, etc

I have a MySQL table with images data. 我有一个包含图像数据的MySQL表。 My backend app is using a live sorting module (drag & drop images). 我的后端应用程序正在使用实时排序模块(拖放图像)。

What I want is to update within a single query all sorted images after sorting process is done, instead of updating single query each time I drop image to a new position (I need to disable any connections while sorting). 我想要的是排序过程完成单个查询中更新所有已排序的图像,而不是每次将图像放到新位置时都更新单个查询(我需要在排序时禁用任何连接)。

As for now, I made this using a foreach loop. 到目前为止,我使用foreach循环进行了此操作。 Compare two arrays - one that holds the original order of the files, and the second one that holds the new order. 比较两个数组-一个保存文件的原始顺序,另一个保存新的顺序。 When the sorting is done it updates only the images with different order. 排序完成后,它仅更新具有不同顺序的图像。 But I'm not sure if the loop is a good place for firing multi-queries (with many images this could drastically overload app) 但是我不确定循环是否是触发多查询的好地方(如果有很多图像,可能会使应用程序严重过载)

public function setSortFilesinSql($new_sort_arr)
{
    $sql = new sql();
    $sql->sqlConnect();
    foreach ($new_sort_arr as $key=>$value){
        if($this->_original_sort[$key] != $value){
            $sql->query("UPDATE images SET sort='".$key."' WHERE name='".$value."' AND full_path='".$this->path."'");
        }
    }
    $this->_original_sort = $new_sort_arr;
    $sql->disconnect();
}

Is there any way to "simlify/speedify" the whole process, or set this as a single query? 有什么方法可以“简化/加快”整个过程,或者将其设置为单个查询?


EDIT 编辑

I've checked CASE clause like this: 我已经检查了CASE子句,如下所示:

public function setSortFilesinSql($new_sort_arr)
{
    $sql = new sql();
    $sql->sqlConnect();
    $sort_query = "UPDATE images SET sort = CASE CONCAT(full_path, '/', name)";
    foreach ($new_sort_arr as $key=>$value){
        if($this->_original_sort[$key] != $value){
            $sort_query .= "WHEN '".$this->path."/".$value."' THEN '".$key."' ";
        }
    }

    $sort_query .= "ELSE sort END CASE";
    $sql->query($sort_query);
    $this->_original_sort = $new_sort_arr;
    $sql->disconnect();

}

Query above gives me following error: 上面的查询给我以下错误:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CASE' 查看与您的MySQL服务器版本相对应的手册以获取在'CASE'附近使用的正确语法

I just write the SQL part, you can build it from PHP: 我只写了SQL部分,您可以从PHP构建它:

UPDATE images
SET sort = CASE name
    WHEN 'x' THEN 1
    WHEN 'y' THEN 2
    ...
    ELSE sort
    END
WHERE full_path='...';

Additionally you can filter the rows in the WHERE clause with AND name in ('x', 'y') . 另外,您可以使用AND name in ('x', 'y')过滤WHERE子句中的行。 In this case you can save the ELSE part in the CASE . 在这种情况下,您可以将ELSE零件保存在CASE中

If you want to use more than one column to identify your row, use this: 如果要使用多个列来标识行,请使用以下命令:

UPDATE images
SET sort = CASE CONCAT(full_path, '/', name)
    WHEN 'path/x' THEN 1
    WHEN 'path/y' THEN 2
    ...
    ELSE sort
    END

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

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