2017-04-11 47 views
0

如果我運行在存儲過程中此查詢,匹配行返回正確問題與傳遞口音到MySQL存儲過程

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」)。

[編輯],它甚至沒有使用硬編碼值

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

任何想法工作? 感謝

回答

0

檢查11.1 Character Set Support在表格中。在Rextester

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) 

實施例:

我不能與字符集和整理在表中使用重現該問題。