I have thousands of rows in a field where many of them contain -
as the last character. I need to use the values as URL so these URLs are broken because of that. Could someone give me the SQL command for phpmyadmin to remove them without removing the other hyphens?
问问题
514 次
2 回答
2
我假设您使用的是 MySQL ...因为您没有指定任何内容。不过,这也可能会移植到其他 SQL 版本。在任何情况下,将其解释为伪 SQL 代码,您应该会有所收获。
尝试这样的事情:
SELECT
CASE WHEN RIGHT(My_Col, 1) = '-' THEN SUBSTR(My_Col, 0, LENGTH(My_Col)-2)
ELSE My_Col AS My_Valid_Link
FROM
My_Table
于 2013-08-21T14:50:07.937 回答
1
这是您使用 MySQL 时需要的 SQL:
UPDATE your_table SET your_url = SUBSTR(your_url, 1, LENGTH(your_url) - 1)
WHERE RIGHT(your_url, 1) = '-'
解释:RIGHT("https://www.example.com/bad-url-", 1) 将等于 "-"。我们找到最后带有连字符的所有网址,并用相同的网址替换坏的网址,但最后没有连字符(如@eatonphil post)
警告:如果your_url
字段上有唯一键,您可能会收到重复键错误。
于 2021-04-29T13:18:39.367 回答