I have a query for SELECT in MariaDB 10.3, which finds all records with and without spaces (accepting accents at the same time). Spaces can be anywhere (after each character, for example).
If I have in the database in the column 'code' value: ŘCT 111 222 33 33
- So I want it to find me the code by entering with spaces ŘCT 111 222 33 33 and without spaces ŘCT1112223333.
- At the same time, I need it to accept diacritics, ie. to find the code by entering both with the diacritics ŘCT1112223333 and without RCT1112223333.
I call SELECT in PHP, so I do chunk_split_unicode() (see below) first and then SELECT with RLIKE, but it does not accept diacritics, if I enter only RCT1112223333, then it will not find anything.
Any easier way?
I will be happy for any advice. Thank you so much.
Martin
$text = "RCT1112223333";
$param = chunk_split_unicode($text, 1, ' ?');
$query = "SELECT * FROM Data WHERE code RLIKE " . $param;
public static function chunk_split_unicode($str, $l = 76, $e = "\r\n") {
if (is_null($str)) {
$str = "";
}
$tmp = array_chunk(preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY), $l);
$str = "";
foreach ($tmp as $t) {
$str .= join("", $t) . $e;
}
// jelikoz se v SQL pouzivaji REGEXP, tak je nutne vyescapovat znaky pouzivane v reg. vyrazech
$str = str_replace(array('(', ')', '[', ']', '{', '}', '*' , '+'), array('\(', '\)', '\[', '\]', '\{', '\}', '\*', '\+'), $str);
return $str;
}
Edit: SQL query
SQL query SHOW CREATE TABLE
data is:
CREATE TABLE `data` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`code` varchar(250) NOT NULL DEFAULT '',
`name` varchar(250) NOT NULL DEFAULT '',
`description` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4282 DEFAULT CHARSET=utf8