将重音传递给mysql存储过程的问题

时间:2017-04-11 15:16:39

标签: mysql stored-procedures procedure diacritics

如果我在存储过程中运行此查询,则会正确返回匹配的行

select brand from article where brand regexp '[àéëË]';

但是,如果我尝试将其转换为动态语句,如

set @s=concat('select brand from article where brand regexp \'[',argument,']\'');
prepare stmt from @s;
execute stmt;

然后当我将'àéëË'作为参数传递给过程时(未找到匹配的行),它失败了。但它没有重音('aeE')。

[edit]它甚至不能使用硬编码值

set @s=concat('select brand from article where brand regexp \'[àéëË]\'');

有什么想法吗? 感谢

1 个答案:

答案 0 :(得分:0)

检查表格中的11.1 Character Set Support

我无法使用表格中使用的字符集和排序规则重现问题:

mysql> DROP PROCEDURE IF EXISTS `sp_test`;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `_article`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `_article` (
    ->     `brand` VARCHAR(255)
    -> ) DEFAULT CHARACTER SET=latin1 COLLATE=latin1_bin;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO `_article`
    ->     (`brand`)
    -> VALUES
    ->     ('àéëË'),
    ->     ('aeE');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> DELIMITER //

mysql> CREATE PROCEDURE `sp_test` (`index` TINYINT, `argument` VARCHAR(255))
    -> BEGIN
    ->     SET @`query` := CONCAT('SELECT ', `index`, ' `index`, `brand`
    '>                      FROM `_article`
    '>                      WHERE `brand` REGEXP \'[', `argument`, ']\'');
    ->     PREPARE `stmt` FROM @`query`;
    ->     EXECUTE `stmt`;
    ->     SET @`query` := NULL;
    ->     DEALLOCATE PREPARE `stmt`;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> CALL `sp_test`(1, 'àéëË');
+-------+----------+
| index | brand    |
+-------+----------+
|     1 | àéëË     |
+-------+----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> CALL `sp_test`(2, 'e');
+-------+-------+
| index | brand |
+-------+-------+
|     2 | aeE   |
+-------+-------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> CALL `sp_test`(3, 'á');
Empty set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> CALL `sp_test`(4, 'A');
Empty set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> CALL `sp_test`(5, 'Ë');
+-------+----------+
| index | brand    |
+-------+----------+
|     5 | àéëË     |
+-------+----------+
1 row in set (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Rextester中的示例。