gpt4 book ai didi

mysql - 从名称列生成 slug 名称列的简单方法?

转载 作者:可可西里 更新时间:2023-11-01 06:40:38 24 4
gpt4 key购买 nike

db_products 表:

| ID | Name         |
| 40 | Foo Bar!~~~~ |

我想生成一个 slug 名称列:

| ID | Name         | Slug_Name |
| 40 | Foo Bar!~~~~ | foo-bar |

这真的可以通过 SQL 完成吗?或者我需要使用不同的语言编写脚本吗?

编辑:我正在使用此函数在 PHP 中生成 slug:

function toSlug($string,$space="-") {
if (function_exists('iconv')) {
$string = @iconv('UTF-8', 'ASCII//TRANSLIT', $string);
}
$string = preg_replace("/[^a-zA-Z0-9 -]/", "", $string);
$string = strtolower($string);
$string = str_replace(" ", $space, $string);
return $string;
}

到目前为止,我的 SQL 技能相当基础。

ALTER TABLE db_products ADD Slug_Name VARCHAR

如何在 SQL 中遍历每一行并设置 Slug_Name = toSlug(Name)

最佳答案

这是一个使用单个查询的简单解决方案:

UPDATE `my_table` SET alias = lower(name),
alias = replace(alias, '.', ' '),
alias = replace(alias, '\'', '-'),
alias = replace(alias,'š','s'),
alias = replace(alias,'Ð','Dj'),
alias = replace(alias,'ž','z'),
alias = replace(alias,'Þ','B'),
alias = replace(alias,'ß','Ss'),
alias = replace(alias,'à','a'),
alias = replace(alias,'á','a'),
alias = replace(alias,'â','a'),
alias = replace(alias,'ã','a'),
alias = replace(alias,'ä','a'),
alias = replace(alias,'å','a'),
alias = replace(alias,'æ','a'),
alias = replace(alias,'ç','c'),
alias = replace(alias,'è','e'),
alias = replace(alias,'é','e'),
alias = replace(alias,'ê','e'),
alias = replace(alias,'ë','e'),
alias = replace(alias,'ì','i'),
alias = replace(alias,'í','i'),
alias = replace(alias,'î','i'),
alias = replace(alias,'ï','i'),
alias = replace(alias,'ð','o'),
alias = replace(alias,'ñ','n'),
alias = replace(alias,'ò','o'),
alias = replace(alias,'ó','o'),
alias = replace(alias,'ô','o'),
alias = replace(alias,'õ','o'),
alias = replace(alias,'ö','o'),
alias = replace(alias,'ø','o'),
alias = replace(alias,'ù','u'),
alias = replace(alias,'ú','u'),
alias = replace(alias,'û','u'),
alias = replace(alias,'ý','y'),
alias = replace(alias,'ý','y'),
alias = replace(alias,'þ','b'),
alias = replace(alias,'ÿ','y'),
alias = replace(alias,'ƒ','f'),
alias = replace(alias, 'œ', 'oe'),
alias = replace(alias, '€', 'euro'),
alias = replace(alias, '$', 'dollars'),
alias = replace(alias, '£', ''),
alias = trim(alias),
alias = replace(alias, ' ', '-'),
alias = replace(alias, '--', '-') ;

在这个例子中:

  • 'my_table'是表名,
  • 'name'为原始字段
  • 'alias' 是我的 slug 字段的名称

希望对您有所帮助!

关于mysql - 从名称列生成 slug 名称列的简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16065288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com