SQL - UPDATE 中每个 SET 命令的 WHERE 子句?
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,您可以使用 CASE 语句。
现在,人们担心与多个
UPDATE
语句相比,一个CASE
语句的可读性较差。这里有一个有效的论点。例如,当更新 1000 行时,使用多个UPDATE
语句感觉和看起来更好,而不是对单个CASE
使用 1000 个不同的条件。然而,有时 CASE 语句更合适。例如,如果您根据某些特征更新行,例如表中字段值的偶数或奇数性质,那么
CASE
语句是更新表中行的一种非常简洁且可维护的方法。表,而不必求助于大量共享特定类型逻辑的UPDATE
语句。以此为例:此表达式采用某个属性的模,当为 0(偶数)时,将值 x 赋给 val,当为 1(奇数)时,将值 y 赋给 val。此语句更新的数据量越大,与多个
UPDATE
语句更新的数据相比,它就越干净。简而言之,
CASE
语句有时与UPDATE
语句一样可读/可维护。这完全取决于你想用它们做什么。编辑:添加 ELSE 子句以更加安全。 OP 可能只对更新特定行感兴趣,因此其余行应保持更新之前的状态。
编辑:添加了一个场景,其中
CASE
语句是比多个UPDATE
语句更有效的方法。Yes, you can with a CASE statement.
Now, there is concern that one
CASE
statement is less readable when compared to severalUPDATE
statements. There is a valid argument here. For example, when 1000 rows are being updated, it just feels and looks better to use severalUPDATE
statements rather than 1000 different conditions to a singleCASE
.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 ofUPDATE
statements that all share a specific type of logic. Take this for example: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 asUPDATE
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 multipleUPDATE
statements.任何 SQL 语句都不能有多个 WHERE 子句,但是您可以使用 CASE 语句来完成您想要执行的操作。您的另一个选择是执行多个 UPDATE 语句。
这是使用 CASE 语句的示例:
这是使用多个 UPDATE 语句的示例:
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:
Here is a sample using multiple UPDATE statements:
没有。进行两次更新:
再考虑一下,您可以使用子查询或 case 语句...
您可能需要将其设为如下子查询:
Nope. Make it two updates:
On second thought, you could use sub-queries or the case statement...
You may need to make that a sub query like this:
一种紧凑且易于扩展的方法:
以这种方式进行查询:
如果处理字符串值,请使用prepare_query 来避免 SQL 语法错误。
A compact and easily scaleable way:
make the query this way:
Use prepare_query to avoid SQL syntax errors if you deal with string values.