Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从name列生成slug name列的简单方法?_Mysql_Sql - Fatal编程技术网

Mysql 从name列生成slug name列的简单方法?

Mysql 从name列生成slug name列的简单方法?,mysql,sql,Mysql,Sql,db_产品表: | ID | Name | | 40 | Foo Bar!~~~~ | 我想生成一个slug name列: | ID | Name | Slug_Name | | 40 | Foo Bar!~~~~ | foo-bar | 这实际上可以通过SQL实现吗?或者我需要使用不同的语言编写脚本 编辑:我使用以下函数在PHP中生成slug: function toSlug($string,$space="-") { if (function_

db_产品
表:

| ID | Name         |
| 40 | Foo Bar!~~~~ |
我想生成一个slug name列:

| 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\u产品添加Slug\u Name VARCHAR


如何循环遍历每一行并在SQL中设置Slug\u Name=toSlug(Name),当然可以使用MySQL进行字符串替换。下面列出了一些您可能会发现有用的字符串函数

SELECT REPLACE('Foo Bar!~~~~', '~', '');
SELECT LOWER('Foo Bar!');
我在MySQL中使用正则表达式时也遇到了这个问题

更新:我提到的博客帖子中的详细信息:

所以我建议创建一个函数来替换正则表达式:

DELIMITER $$
FUNCTION `regex_replace`(pattern varchar(1000),replacement varchar(1000),original varchar(1000))
RETURNS varchar(1000)
DETERMINISTIC
BEGIN
DECLARE temp VARCHAR(1000);
DECLARE ch VARCHAR(1);
DECLARE i INT;
SET i = 1;
SET temp = original;
IF original REGEXP pattern THEN
    SET temp = "";
    loop_label: LOOP
    IF i>CHAR_LENGTH(original) THEN
        LEAVE loop_label;
    END IF;
    SET ch = SUBSTRING(original,i,1);
    IF NOT ch REGEXP pattern THEN
        SET temp = CONCAT(temp,ch);
    ELSE
        SET temp = CONCAT(temp,replacement);
    END IF;
    SET i=i+1;
END LOOP;
END IF;
RETURN temp;
END$$
DELIMITER ;
然后是类似于以下的东西

SELECT regex_replace('[^a-zA-Z0-9]+', '', '%$&?/’|test><+-,][)(' )

下面是一个简单的解决方案,只需一个查询:

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字段的名称

希望有帮助

我对Erwan Dupeux Maire的答案做了一些编辑,以解释我在
名称
字段中缺少的一些字符:'、'、'&'和'/'

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

更改您的tbl\u名称字段名称slug\u字段名称

SELECT field_name,
 REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
 REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
 REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
 LOWER(TRIM(field_name)), ':', ''), ')', ''), '(', ''), ',', ''), '\\', ''), '\/', ''), '\"', ''), '?', ''), '\'', ''), '&', ''), '!', ''), '.', ''), ' ', '-'), '--', '-'), '--', '-')
AS `slug_field_name`
FROM tbl_name
对于测试“您的字符串”“结果您的字符串”

SELECT
 REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
 REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
 REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
 LOWER(TRIM('Your String')), ':', ''), ')', ''), '(', ''), ',', ''), '\\', ''), '\/', ''), '\"', ''), '?', ''), '\'', ''), '&', ''), '!', ''), '.', ''), ' ', '-'), '--', '-'), '--', '-')
AS `slug_test`
对于特殊字符:

附加一个[替换(]和一个[,':','')]

例如,此字符串中的字符“你好,schöne Welt

结果是“哈罗·肖恩世界奖”


到目前为止你试过什么?MySQL有几个字符串操作函数。您可能需要特别了解replace()、lower()、trim()和regexp()。与
TRIGGER
结合使用可能非常有效。从未使用过一个,但似乎它应该类似于
CREATE TRIGGER BEFORE INSERT FOR EACH ROW SET new.slug=COOL\u REPLACE\u函数(new.title)SELECT
 REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
 REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
 REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
 LOWER(TRIM('Your String')), ':', ''), ')', ''), '(', ''), ',', ''), '\\', ''), '\/', ''), '\"', ''), '?', ''), '\'', ''), '&', ''), '!', ''), '.', ''), ' ', '-'), '--', '-'), '--', '-')
AS `slug_test`
SELECT
 REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
 REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
 REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
 REPLACE(
 LOWER(TRIM('Hallo schöne Welt')), 'ö', 'o'), ':', ''), ')', ''), '(', ''), ',', ''), '\\', ''), '\/', ''), '\"', ''), '?', ''), '\'', ''), '&', ''), '!', ''), '.', ''), ' ', '-'), '--', '-'), '--', '-')
AS `slug_test`