SQL - UPDATE 中每个 SET 命令的 WHERE 子句?

发布于 2024-08-04 14:37:36 字数 225 浏览 5 评论 0原文

我正在尝试在 PHP 中创建 SQL 查询来更新表。 是否可以为每个受影响的行使用不同的 WHERE 子句?

例如:

UPDATE table 
SET val=X WHERE someproperty = 1,
SET val=Y WHERE someproperty = 2

等等?

任何帮助表示赞赏。谢谢

I'm trying to create an SQL query in PHP to update a table.
Is it possible to have a different WHERE clause for each affected row?

eg something like:

UPDATE table 
SET val=X WHERE someproperty = 1,
SET val=Y WHERE someproperty = 2

etc?

Any help appreciated. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

杀お生予夺 2024-08-11 14:37:36

是的,您可以使用 CASE 语句。

UPDATE table
SET val = CASE someproperty  
           WHEN 1 THEN x 
           WHEN 2 THEN y
           ....
          ELSE
           val
          END

现在,人们担心与多个 UPDATE 语句相比,一个 CASE 语句的可读性较差。这里有一个有效的论点。例如,当更新 1000 行时,使用多个 UPDATE 语句感觉和看起来更好,而不是对单个 CASE 使用 1000 个不同的条件。

然而,有时 CASE 语句更合适。例如,如果您根据某些特征更新行,例如表中字段值的偶数或奇数性质,那么 CASE 语句是更新表中行的一种非常简洁且可维护的方法。表,而不必求助于大量共享特定类型逻辑的 UPDATE 语句。以此为例:

UPDATE table
SET val = CASE MOD(someproperty, 2)  
           WHEN 0 THEN x 
           WHEN 1 THEN y
          END

此表达式采用某个属性的模,当为 0(偶数)时,将值 x 赋给 val,当为 1(奇数)时,将值 y 赋给 val。此语句更新的数据量越大,与多个 UPDATE 语句更新的数据相比,它就越干净。

简而言之,CASE 语句有时与 UPDATE 语句一样可读/可维护。这完全取决于你想用它们做什么。

编辑:添加 ELSE 子句以更加安全。 OP 可能只对更新特定行感兴趣,因此其余行应保持更新之前的状态。

编辑:添加了一个场景,其中 CASE 语句是比多个 UPDATE 语句更有效的方法。

Yes, you can with a CASE statement.

UPDATE table
SET val = CASE someproperty  
           WHEN 1 THEN x 
           WHEN 2 THEN y
           ....
          ELSE
           val
          END

Now, there is concern that one CASE statement is less readable when compared to several UPDATE statements. There is a valid argument here. For example, when 1000 rows are being updated, it just feels and looks better to use several UPDATE statements rather than 1000 different conditions to a single CASE.

However, sometimes a CASE statement is more appropriate. If, for example, you are updating rows based on some trait, say the even or odd nature of a field's value the table, then a CASE statement is a wonderfully concise and maintainable way to update rows in the table without having to resort to a huge number of UPDATE statements that all share a specific type of logic. Take this for example:

UPDATE table
SET val = CASE MOD(someproperty, 2)  
           WHEN 0 THEN x 
           WHEN 1 THEN y
          END

This expression takes the modulus of someproperty and, when 0 (even), assigns value x to val and, when 1 (odd), assigns value y to val. The greater the volume of data being updated by this statement, the cleaner it is compared to doing so by multiple UPDATE statements.

In short, CASE statements are sometimes just as readable/maintainable as UPDATE statements. It all depends on what you are trying to do with them.

EDIT: Added the ELSE clause to be extra safe. The OP may be interested in updating only specific rows so the rest should remain as they prior to the UPDATE.

EDIT: Added a scenario where the CASE statement is a more effective approach than multiple UPDATE statements.

初吻给了烟 2024-08-11 14:37:36

任何 SQL 语句都不能有多个 WHERE 子句,但是您可以使用 CASE 语句来完成您想要执行的操作。您的另一个选择是执行多个 UPDATE 语句。

这是使用 CASE 语句的示例:

UPDATE table
SET val = (
    CASE someproperty
        WHEN 1 THEN X
        WHEN 2 THEN Y
        ELSE val
    END
);

这是使用多个 UPDATE 语句的示例:

UPDATE table SET val=X WHERE someproperty = 1;
UPDATE table SET val=Y WHERE someproperty = 2;

You cannot have multiple WHERE clauses for any SQL statement, however you can use a CASE statement to accomplish what you are trying to do. Another option that you have is to execute multiple UPDATE statements.

Here is a sample using the CASE statement:

UPDATE table
SET val = (
    CASE someproperty
        WHEN 1 THEN X
        WHEN 2 THEN Y
        ELSE val
    END
);

Here is a sample using multiple UPDATE statements:

UPDATE table SET val=X WHERE someproperty = 1;
UPDATE table SET val=Y WHERE someproperty = 2;
陈甜 2024-08-11 14:37:36

没有。进行两次更新:

UPDATE table SET val=X WHERE someproperty = 1;
UPDATE table SET val=Y WHERE someproperty = 2;

再考虑一下,您可以使用子查询或 case 语句...

UPDATE table SET val= ( case when someproperty = 1 then X when someproperty = 2 then Y else val END )

您可能需要将其设为如下子查询:

UPDATE table t1 SET val = ( select CASE when someproperty = 1 then X when someproperty = 2 then Y ELSE val END from table t2 where t1.primarykey = t2.primary key )

Nope. Make it two updates:

UPDATE table SET val=X WHERE someproperty = 1;
UPDATE table SET val=Y WHERE someproperty = 2;

On second thought, you could use sub-queries or the case statement...

UPDATE table SET val= ( case when someproperty = 1 then X when someproperty = 2 then Y else val END )

You may need to make that a sub query like this:

UPDATE table t1 SET val = ( select CASE when someproperty = 1 then X when someproperty = 2 then Y ELSE val END from table t2 where t1.primarykey = t2.primary key )
满栀 2024-08-11 14:37:36
UPDATE TABLE
SET VAL CASE SOMEPROPERTY WHEN 1 THEN X WHEN 2 THEN Y END
UPDATE TABLE
SET VAL CASE SOMEPROPERTY WHEN 1 THEN X WHEN 2 THEN Y END
往事随风而去 2024-08-11 14:37:36

一种紧凑且易于扩展的方法:

UPDATE table1 SET val=ELT(FIND_IN_SET(someproperty, '1, 2'), X, Y);

以这种方式进行查询:

$condition = array(1, 2);
$newvals = array('X', 'Y');
$query = "UPDATE table1 SET val=ELT(FIND_IN_SET(someproperty, '". implode(',', $condition). "', ". implode(', ', $newvals). ")";

如果处理字符串值,请使用prepare_query 来避免 SQL 语法错误。

A compact and easily scaleable way:

UPDATE table1 SET val=ELT(FIND_IN_SET(someproperty, '1, 2'), X, Y);

make the query this way:

$condition = array(1, 2);
$newvals = array('X', 'Y');
$query = "UPDATE table1 SET val=ELT(FIND_IN_SET(someproperty, '". implode(',', $condition). "', ". implode(', ', $newvals). ")";

Use prepare_query to avoid SQL syntax errors if you deal with string values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文