编辑列中的文本

发布于 2024-09-16 14:26:29 字数 338 浏览 2 评论 0原文

只是想知道是否有人可以帮助我解决以下问题。

我在 MySQL 中有一个数据库,我正在使用 PHPMyAdmin 与数据库交互。

因此,在表 example_table_name 中,我有两列 product_item_shortproduct_item_long,其中包含 URL。现在有 3000 行数据,作为示例,每行中的 URL 以 data/image/someimage.png 开头。

在这两列中,我都需要删除 data/ 并且我想知道如何在 SQL 中做到这一点。

非常感谢

Just wondering if someone can assist me with the following issue.

I have a database in MySQL and I'm using PHPMyAdmin to interact with the database.

So within the table example_table_name I have two columns product_item_short and product_item_long which have URL's in them. Now there is 3000 rows of data and as an example the URL in each starts with data/image/someimage.png.

In both columns I need to remove the data/ and I would like to know how I could so this in SQL.

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

云归处 2024-09-23 14:26:29

您可以使用 SUBSTR()功能:

UPDATE your_table 
SET    product_item_short = SUBSTR(product_item_short, 6),
       product_item_long = SUBSTR(product_item_long, 6);

测试用例:

CREATE TABLE your_table (
    id int, 
    product_item_short varchar(50), 
    product_item_long varchar(50)
);


INSERT INTO your_table VALUES (1, 'data/image/someimage.png',         'data/image/someimage.png');
INSERT INTO your_table VALUES (2, 'data/other-folder/someimage.png',  'data/other-folder/someimage.png');
INSERT INTO your_table VALUES (3, 'data/no-folder.png',               'data/no-folder.png');
INSERT INTO your_table VALUES (4, 'data/image/path/three-levels.png', 'data/image/path/three-levels.png');

UPDATE 后的结果:

SELECT * FROM your_table;
+------+-----------------------------+-----------------------------+
| id   | product_item_short          | product_item_long           |
+------+-----------------------------+-----------------------------+
|    1 | image/someimage.png         | image/someimage.png         |
|    2 | other-folder/someimage.png  | other-folder/someimage.png  |
|    3 | no-folder.png               | no-folder.png               |
|    4 | image/path/three-levels.png | image/path/three-levels.png |
+------+-----------------------------+-----------------------------+
4 rows in set (0.00 sec)

UPDATE:

如果您的解决方案消除了路径的第一个目录,无论是 data/anything-else/,您可能需要使用 LOCATE() 函数,如 @Frank 在另一个答案中建议

UPDATE your_table 
SET    product_item_short = SUBSTR(product_item_short, LOCATE('/', product_item_short) + 1),
       product_item_long = SUBSTR(product_item_long, LOCATE('/', product_item_long) + 1);

You can use the SUBSTR() function:

UPDATE your_table 
SET    product_item_short = SUBSTR(product_item_short, 6),
       product_item_long = SUBSTR(product_item_long, 6);

Test case:

CREATE TABLE your_table (
    id int, 
    product_item_short varchar(50), 
    product_item_long varchar(50)
);


INSERT INTO your_table VALUES (1, 'data/image/someimage.png',         'data/image/someimage.png');
INSERT INTO your_table VALUES (2, 'data/other-folder/someimage.png',  'data/other-folder/someimage.png');
INSERT INTO your_table VALUES (3, 'data/no-folder.png',               'data/no-folder.png');
INSERT INTO your_table VALUES (4, 'data/image/path/three-levels.png', 'data/image/path/three-levels.png');

Result after UPDATE:

SELECT * FROM your_table;
+------+-----------------------------+-----------------------------+
| id   | product_item_short          | product_item_long           |
+------+-----------------------------+-----------------------------+
|    1 | image/someimage.png         | image/someimage.png         |
|    2 | other-folder/someimage.png  | other-folder/someimage.png  |
|    3 | no-folder.png               | no-folder.png               |
|    4 | image/path/three-levels.png | image/path/three-levels.png |
+------+-----------------------------+-----------------------------+
4 rows in set (0.00 sec)

UPDATE:

If you a solution that eliminates the first directory of the path, whether it is data/ or anything-else/, you may want to use the LOCATE() function, as @Frank's suggested in another answer:

UPDATE your_table 
SET    product_item_short = SUBSTR(product_item_short, LOCATE('/', product_item_short) + 1),
       product_item_long = SUBSTR(product_item_long, LOCATE('/', product_item_long) + 1);
对风讲故事 2024-09-23 14:26:29
UPDATE tablename
SET product_item_short = REPLACE(product_item_short, 'data/image/someimage.png', 'newurl/something.png');
UPDATE tablename
SET product_item_short = REPLACE(product_item_short, 'data/image/someimage.png', 'newurl/something.png');
转身泪倾城 2024-09-23 14:26:29

如果你想比丹尼尔在他的答案中描述的更灵活,你可以使用类似的东西

SUBSTR(product_item_short, LOCATE('/', product_item_short) + 1)

来切断第一个斜杠之后的所有内容。

If you want to be more flexible than what Daniel described in his answer, you can use something like

SUBSTR(product_item_short, LOCATE('/', product_item_short) + 1)

which would cut off everything after the first slash.

魄砕の薆 2024-09-23 14:26:29
UPDATE example_table
SET product_item_short = SUBSTRING(product_item_short,6),
    product_item_long = SUBSTRING(product_item_long,6);
UPDATE example_table
SET product_item_short = SUBSTRING(product_item_short,6),
    product_item_long = SUBSTRING(product_item_long,6);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文