是否有允许 MySQL “忽略"的技巧?UPDATE 语句的 SET 子句中的尾随逗号? [英] Is there a trick to allow MySQL to "ignore" a trailing comma in the SET clause of an UPDATE statement?

查看:60
本文介绍了是否有允许 MySQL “忽略"的技巧?UPDATE 语句的 SET 子句中的尾随逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 C++ 中,我以编程方式生成 UPDATE 语句,这使得剥离尾随逗号变得困难:

From C++, I'm generating an UPDATE statement programmatically in a way that makes stripping a trailing comma difficult:

UPDATE `myTable` SET 
   `Field1` = "value",
   `Field2` = "value",
   `Field3` = "value",
WHERE `Field4` = "value";

是否有一些静态的、无操作的键/值对我可以在最终列值规范之后插入,这将使尾随逗号可以"?或者我是否必须使我的 C++ 代码复杂化以避免完全编写它?

Is there some static, no-op key/value pair I can insert after the final column value specification, which would make the trailing comma "okay"? Or will I have to complicate my C++ code to avoid writing it entirely?

显然等同于以下无效方法的东西会很好.

Something apparently equivalent to the following invalid approach would be nice.

UPDATE `myTable` SET 
   `Field1` = "value",
   `Field2` = "value",
   `Field3` = "value",
--- 1 = 1
WHERE `Field4` = "value";

推荐答案

除非您愿意像 Igoel 建议的那样复制一个值(如果值很长,这可能不理想),简单的答案是 没有.

Unless you're willing to duplicate a value as Igoel suggests (which may not be ideal if the value is lengthy), the simple answer is no.

一个简短的令人鼓舞的可能性是使用别名NEW 来表示传入的值,以便可以复制最终值而不必再次在查询中呈现它(我希望这会被查询优化器删除):

One briefly encouraging possibility was the use of the alias NEW to represent the incoming values, so that the final value may be duplicated without actually having to present it in the query again (and I'd hope that this would be taken out by the query optimiser):

UPDATE `myTable` SET 
   `Field1` = "value",
   `Field2` = "value",
   `Field3` = "value",
--- `Field3` = NEW.`Field3`
WHERE `Field4` = "value";

唉,这在 UPDATE 语句中不受支持,仅在触发器主体内.

Alas, this is not supported in an UPDATE statement, only inside a trigger body.

在执行语句之前,您必须在 C++ 中进行操作,通过字符替换(, for )或字符删除;前者可能会生成比您现在更复杂的代码,后者可能效率低下(取决于您的查询构建代码的结构),但它仍然是您最好的选择.

You'll have to do the manipulation in your C++ before executing the statement, either through character replacement (, for ) or character removal; the former may produce more complex code than you have now, and the latter may be inefficient (depending on the structure of your query-building code), but it's still your best bet.

这篇关于是否有允许 MySQL “忽略"的技巧?UPDATE 语句的 SET 子句中的尾随逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆