Mysql:修剪数据库中的所有字段
UPDATE mytable SET mycolumn= LTRIM(RTRIM(mycolumn));
修剪列删除拖车空间效果很好,但是我如何调整它以修剪所有列而不必在表中写入每个列名称?因为我有一个巨大的数据库。
UPDATE mytable SET mycolumn= LTRIM(RTRIM(mycolumn));
works fine on trimming columns removing trailer spaces, but how can i adjust it to trim all columns without having to write each column name in table ?? cause i kind have a huge database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
晚了几年,但可能对其他人有帮助:
此代码修剪表
your_table
的所有字段。可以扩展到以同样的方式处理整个数据库......
Some years late, but might help others:
This code trims all fields of a the table
your_table
.Could be expanded to work on the whole database in the same way....
您扩展每列的查询:
you expand the query for each column:
由于问题要求整个数据库,因此这里是生成所需 SQL 的脚本。我跳过自动执行,按照你喜欢的方式执行。
@ZweiStein 谢谢。
Since the question asks for the whole database, here is the script that generates the required SQL. I skip the auto execute, execute it as you like.
@ZweiStein Thanks.
等等。
and so on, and so forth.
如果列不是太多,您可以
直接地
通过
TRIM()
函数按 your_column_name 进行UPDATE
:否则,ZweiStein 的< /a> 回答上面针对单个表的问题,
或者Izhar Aazmi的对整个数据库的回答似乎是可行的方法。
Hiram 的对另一篇 SO 帖子的回答包括对仅 TRIM VARCHAR 领域:优秀的功能!
或者,如果使用 T-SQL 或其他不支持
TRIM
的语言,请使用LTRIM(RTRIM(...))
技巧,由吉姆·鲁宾斯坦和丹尼斯建议上面的德伯纳迪。
If there are not too many columns, you could just
directly
UPDATE
each by your_column_name, via theTRIM()
function:Otherwise, ZweiStein's answer above for a single table,
or Izhar Aazmi's answer for an entire database seem the way to go.
Hiram's answer to another SO Post includes a check to only TRIM VARCHAR fields: excellent feature!
Or, if using T-SQL, or others which do not support
TRIM
, use theLTRIM(RTRIM(...))
trick,suggested by Jim Rubenstein and Denis de Bernardy above.
当我遇到这个问题时,我实际上正在寻找类似的遗留表,该表不断由外部源更新。我意识到OP正在寻找一个纯粹的SQL(MySQL)答案,但如果你使用Rails,你可能会发现我想出的这个花絮很有帮助:
你也可以将它包装到模型中的类方法中
然后像这样调用它这
I was actually looking for something similar for a legacy table that's constantly updated by an outside source when I came across this question. I realize the OP was looking for a purely SQL(MySQL) answer, but in case you use Rails, you might find this tidbit that I came up with helpful:
You can also wrap it into a class method in your model
Then call it like this
您可以使用 PHP(为了避免 sql 错误,最好打印查询然后稍后执行它们):
?>
You can use PHP for it ( in order to avoid sql errors, better print queries then execute them later ) :
?>