MySQL 将文本列中的值移动到新的 INT 列

MySQL move value from within text column to new INT column

所以,我想为 MyBB 改进一个 shoutbox 插件,并在 pMA 中执行以下操作。

私人喊话存储为TEXT/VARCHAR:"/pvt userid message"。我想要实现的是:

之前使用的代码 sscanf($message, "/pvt %d", $userID)。 查询类似于 UPDATE shouts SET touid=???, message=??? WHERE message LIKE '/pvt %'.

@edit:示例:

/pvt 55 Hello world - this is a private shout!

/pvt 675 Another pm.

This is not a private shout

第一部分没那么难:

update shouts
    set touid = substring_index(message, '/pvt ', -1) + 0
    where message LIKE '/pvt %';

这会拆分 '/pvt ' 上的字符串,获取第二部分并将其转换为整数。 MySQL 进行静默转换,因此它转换前导数字。

替代方案利用了模式在开头的事实:

update shouts
    set touid = substr(message, 6, 1000) + 0
    where message LIKE '/pvt %';

您可以通过将其替换为任何内容来删除字符串的这一部分:

update shouts
    set touid = substring_index(message, '/pvt ', -1) + 0
        message = replace(message,
                          concat('/pvt ', substring_index(message, '/pvt ', -1) + 0),
                          '')
    where message LIKE '/pvt %';