雪地
发布于

如何在 MySQL 中设置数字通配符?

尝试创建一个 SQL 脚本,用于在两个不同的数据库中查找代码。设置一个带有数字通配符的参数。
代码通常写成这样,例如 将参数设置为 as 以返回以 ABC 开头的代码的所有可能变体,但这导致输出为空 ABC108ABC109DEF47213ABC[0-9]+

尝试了以下操作,期望输出在两个数据库中制作的最新代码:

  SET @investment_code = 'ABC[0-9]+';

SELECT 
    investment_name, 
    i.service_client_id, 
    currency_symbol, 
    investment_code C_investment_codes
FROM client.investments i
LEFT JOIN client.currencies c
    ON i.currency_id = c.id
WHERE i.investment_code COLLATE utf8mb4_unicode_ci LIKE @investment_code
ORDER BY investment_code DESC
LIMIT 1;

SELECT 
`key`, 
service_client_id, 
`value` SD_investment_codes
FROM client_data.standing_data sd
WHERE sd.`value` COLLATE utf8mb4_unicode_ci LIKE @investment_code
ORDER BY `value` DESC
LIMIT 1;

这导致 output 为空。当使用时,得到了结果,但特别想将数字的参数设置为遵循 ABC。这是预期输出:SET @investment_code = 'ABC%';

浏览 (15)
点赞
收藏
1条评论
Klustron小助手
在 MySQL 中,LIKE 运算符不直接支持正则表达式。相反,可以使用 % 通配符来匹配任何字符序列。 对于您的脚本,您可以利用 MySQL 中提供的 REGEXP 运算符,它允许您使用正则表达式。下面是一个示例 SET @investment_code = '^ABC[0-9]+$'; SELECT investment_name, i.service_client_id, currency_symbol, investment_code AS C_investment_codes FROM client.investments i LEFT JOIN client.currencies c ON i.currency_id = c.id WHERE i.investment_code REGEXP @investment_code ORDER BY investment_code DESC LIMIT 1; table SELECT `key`, service_client_id, `value` AS SD_investment_codes FROM client_data.standing_data sd WHERE sd.`value` REGEXP @investment_code ORDER BY `value` DESC LIMIT 1;
点赞
评论