gpt4 book ai didi

MySQL CONCAT 2 行到 1 行尾随\有时

转载 作者:行者123 更新时间:2023-11-29 02:50:48 27 4
gpt4 key购买 nike

我正在尝试将不同表中的 CONCAT 2 行合并为 1 行。问题是 CONCAT 位于目录和文件路径中,有时尾随“\”。我想知道使用 MySQL 执行此操作的最快方法是什么。

INSERT INTO [database.tableName] (full_path) SELECT CONCAT(A.Loc_Path, '\', A.File_Path) FROM [database.tableName2] as A where ID > 0;

目前这有时会给我正确的答案。问题是当 Loc_Path 有尾随 \ 时。

例子:

Loc_Path      |    File_Path
c:\test | yay.txt
c:\test\ | yay.txt

这个结果是:

c:\test\yay.txt     [GOOD]
c:\test\\yay.txt [BAD]

最佳答案

您可以使用 case 语句来查看 Loc_Path 是否以 \ 结尾。

SELECT CASE WHEN Loc_Path LIKE '%\\' 
THEN CONCAT(A.Loc_Path, A.File_Path)
ELSE CONCAT(A.Loc_Path, '\\', A.File_Path)
END AS Result
FROM tableName2
WHERE ID > 0

或者使用 SUBSTRING 而不是 LIKE

SELECT CASE WHEN substring(Loc_Path , (char_length(Loc_Path ) - 1)) = '\\' 
THEN CONCAT(A.Loc_Path, A.File_Path)
ELSE CONCAT(A.Loc_Path, '\\', A.File_Path)
END AS Result
FROM tableName2
WHERE ID > 0

别忘了 escape your backslash .

关于MySQL CONCAT 2 行到 1 行尾随\有时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36181993/

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