Mysql:修剪数据库中的所有字段

发布于 2024-11-08 16:22:31 字数 158 浏览 0 评论 0原文

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 技术交流群。

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

发布评论

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

评论(7

放赐 2024-11-15 16:22:31

晚了几年,但可能对其他人有帮助:
此代码修剪表 your_table所有字段。
可以扩展到以同样的方式处理整个数据库......

SET SESSION group_concat_max_len = 1000000;
SELECT concat('update your_table set ',
    group_concat(concat('`',COLUMN_NAME, '` = trim(`',COLUMN_NAME,'`)')),';')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'your_table'
INTO @trimcmd;
    
PREPARE s1 from @trimcmd;
EXECUTE s1;
DEALLOCATE PREPARE s1;

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....

SET SESSION group_concat_max_len = 1000000;
SELECT concat('update your_table set ',
    group_concat(concat('`',COLUMN_NAME, '` = trim(`',COLUMN_NAME,'`)')),';')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'your_table'
INTO @trimcmd;
    
PREPARE s1 from @trimcmd;
EXECUTE s1;
DEALLOCATE PREPARE s1;
冰火雁神 2024-11-15 16:22:31

您扩展每列的查询:

UPDATE mytable
SET mycolumn = LTRIM(RTRIM(mycolumn)),
    mycolumn2 = LTRIM(RTRIM(mycolumn2)),
    ...;

you expand the query for each column:

UPDATE mytable
SET mycolumn = LTRIM(RTRIM(mycolumn)),
    mycolumn2 = LTRIM(RTRIM(mycolumn2)),
    ...;
魔法少女 2024-11-15 16:22:31

由于问题要求整个数据库,因此这里是生成所需 SQL 的脚本。我跳过自动执行,按照你喜欢的方式执行。

-- Set your database name here
SET @my_database:='YOUR_DB_NAME';

SET SESSION group_concat_max_len = 1000000;

SELECT 
    CONCAT('UPDATE `', @my_database, '`.`', TABLE_NAME, 
            '` SET ', GROUP_CONCAT(
                CONCAT('`', COLUMN_NAME, '` = TRIM(`', COLUMN_NAME, '`)')
                ORDER BY ORDINAL_POSITION ASC),
            ';') AS `query`
FROM
    INFORMATION_SCHEMA.COLUMNS
WHERE
    TABLE_SCHEMA = @my_database
GROUP BY TABLE_NAME
ORDER BY TABLE_NAME ASC;

@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.

-- Set your database name here
SET @my_database:='YOUR_DB_NAME';

SET SESSION group_concat_max_len = 1000000;

SELECT 
    CONCAT('UPDATE `', @my_database, '`.`', TABLE_NAME, 
            '` SET ', GROUP_CONCAT(
                CONCAT('`', COLUMN_NAME, '` = TRIM(`', COLUMN_NAME, '`)')
                ORDER BY ORDINAL_POSITION ASC),
            ';') AS `query`
FROM
    INFORMATION_SCHEMA.COLUMNS
WHERE
    TABLE_SCHEMA = @my_database
GROUP BY TABLE_NAME
ORDER BY TABLE_NAME ASC;

@ZweiStein Thanks.

Hello爱情风 2024-11-15 16:22:31
UPDATE mytable SET 
mycolumn = LTRIM(RTRIM(mycolumn)), 
mycolumn2 = LTRIM(RTRIM(mycolumn2)) 

等等。

UPDATE mytable SET 
mycolumn = LTRIM(RTRIM(mycolumn)), 
mycolumn2 = LTRIM(RTRIM(mycolumn2)) 

and so on, and so forth.

2024-11-15 16:22:31

如果列不是太多,您可以
直接地
通过 TRIM() 函数按 your_column_name 进行UPDATE

UPDATE mytable SET 
mycolumn1 = TRIM(mycolumn1), 
mycolumn2 = TRIM(mycolumn2),
mycolumn3 = TRIM(mycolumn3),
mycolumn4 = TRIM(mycolumn4)

否则,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 the TRIM() function:

UPDATE mytable SET 
mycolumn1 = TRIM(mycolumn1), 
mycolumn2 = TRIM(mycolumn2),
mycolumn3 = TRIM(mycolumn3),
mycolumn4 = TRIM(mycolumn4)

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 the LTRIM(RTRIM(...)) trick,
suggested by Jim Rubenstein and Denis de Bernardy above.

乖乖公主 2024-11-15 16:22:31

当我遇到这个问题时,我实际上正在寻找类似的遗留表,该表不断由外部源更新。我意识到OP正在寻找一个纯粹的SQL(MySQL)答案,但如果你使用Rails,你可能会发现我想出的这个花絮很有帮助:

MyModel.update_all(MyModel.columns.map(&:name).map{|x| "#{x} = TRIM(#{x})"}.join(', '))

你也可以将它包装到模型中的类方法中

class MyModel < ActiveRecord::Base
  def self.trim_all
   update_all(columns.map(&:name).map{|x| "#{x} = TRIM(#{x})"}.join(', '))
 end
end

然后像这样调用它这

MyModel.trim_all

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:

MyModel.update_all(MyModel.columns.map(&:name).map{|x| "#{x} = TRIM(#{x})"}.join(', '))

You can also wrap it into a class method in your model

class MyModel < ActiveRecord::Base
  def self.trim_all
   update_all(columns.map(&:name).map{|x| "#{x} = TRIM(#{x})"}.join(', '))
 end
end

Then call it like this

MyModel.trim_all
筑梦 2024-11-15 16:22:31

您可以使用 PHP(为了避免 sql 错误,最好打印查询然后稍后执行它们):

$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'database';
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
$db->set_charset("utf8");

$queries = '';
$query="SELECT * from table";
$result = $db->query($query);
$headers = $result->fetch_fields();
foreach($headers as $header) {
        $col = $header->name;

       $queries .= "UPDATE table SET `".$col."` = TRIM(`".$col."`) </br>";
}
echo $queries;

?>

You can use PHP for it ( in order to avoid sql errors, better print queries then execute them later ) :

$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'database';
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
$db->set_charset("utf8");

$queries = '';
$query="SELECT * from table";
$result = $db->query($query);
$headers = $result->fetch_fields();
foreach($headers as $header) {
        $col = $header->name;

       $queries .= "UPDATE table SET `".$col."` = TRIM(`".$col."`) </br>";
}
echo $queries;

?>

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