4

我有一个名为“city”的表,列名为“city_name”,大约有 200 条记录。

我创建了另一个名为 slugs 的列,我想在同一行中复制“city_name”中的所有记录,并将空格替换为破折号 - 和小写。

我怎样才能通过 phpmyadmin 实现这一点。

谢谢

4

2 回答 2

14

You should be able to do this via the following query:

UPDATE city SET slugs=LOWER(REPLACE(city_name, " ", "-"))

Breaking this down, we're using REPLACE to swap all instances of " " with "-" in the existing city_name column, and then passing the result of this to the LOWER function (to convert the data to lower case) before setting the slugs field with this value.

Depending on how "clean" your data is, you might also want to TRIM the data (to remove any leading or trailing spaces in the city_name field) before you apply the REPLACE as such:

UPDATE city SET slugs=LOWER(REPLACE(TRIM(city_name), " ", "-"))

Incidentally, if you've not used (My)SQL much I'd recommend a read of the String Functions manual page - the time you spend on this now will more than repay itself in the future.

于 2013-08-25T09:55:43.727 回答
0

Here is SQLFiddle

MYSql documentation for function LOWER(str) and REPLACE(str,from_str,to_str)

UPDATE city SET slugs = LOWER(REPLACE(city_name," ", "-"));
于 2013-08-25T09:57:07.050 回答