how to solve access denied into my mysql database - mysql

I don't know when I run the following code in phpMyAdmin
SET GLOBAL log_bin_trust_function_creators = 1;
-- ----------------------------
-- Function structure for `__mydiv`
-- ----------------------------
DROP FUNCTION IF EXISTS `__mydiv`;
DELIMITER ;;
CREATE FUNCTION `__mydiv`(`a` int, `b` int) RETURNS bigint(20)
BEGIN
return FLOOR(a / b);
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `__mymod`
-- ----------------------------
DROP FUNCTION IF EXISTS `__mymod`;
DELIMITER ;;
CREATE FUNCTION `__mymod`(`a` int, `b` int) RETURNS bigint(20)
BEGIN
return (a - b * FLOOR(a / b));
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `_gdmarray`
-- ----------------------------
DROP FUNCTION IF EXISTS `_gdmarray`;
DELIMITER ;;
CREATE FUNCTION `_gdmarray`(`m` smallint) RETURNS smallint(2)
BEGIN
CASE m
WHEN 0 THEN RETURN 31;
WHEN 1 THEN RETURN 28;
WHEN 2 THEN RETURN 31;
WHEN 3 THEN RETURN 30;
WHEN 4 THEN RETURN 31;
WHEN 5 THEN RETURN 30;
WHEN 6 THEN RETURN 31;
WHEN 7 THEN RETURN 31;
WHEN 8 THEN RETURN 30;
WHEN 9 THEN RETURN 31;
WHEN 10 THEN RETURN 30;
WHEN 11 THEN RETURN 31;
END CASE;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `_jdmarray`
-- ----------------------------
DROP FUNCTION IF EXISTS `_jdmarray`;
DELIMITER ;;
CREATE FUNCTION `_jdmarray`(`m` smallint) RETURNS smallint(2)
BEGIN
CASE m
WHEN 0 THEN RETURN 31;
WHEN 1 THEN RETURN 31;
WHEN 2 THEN RETURN 31;
WHEN 3 THEN RETURN 31;
WHEN 4 THEN RETURN 31;
WHEN 5 THEN RETURN 31;
WHEN 6 THEN RETURN 30;
WHEN 7 THEN RETURN 30;
WHEN 8 THEN RETURN 30;
WHEN 9 THEN RETURN 30;
WHEN 10 THEN RETURN 30;
WHEN 11 THEN RETURN 29;
END CASE;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `_jdmarray2`
-- ----------------------------
DROP FUNCTION IF EXISTS `_jdmarray2`;
DELIMITER ;;
CREATE FUNCTION `_jdmarray2`(`m` smallint) RETURNS smallint(2)
BEGIN
CASE m
WHEN 1 THEN RETURN 31;
WHEN 2 THEN RETURN 31;
WHEN 3 THEN RETURN 31;
WHEN 4 THEN RETURN 31;
WHEN 5 THEN RETURN 31;
WHEN 6 THEN RETURN 31;
WHEN 7 THEN RETURN 30;
WHEN 8 THEN RETURN 30;
WHEN 9 THEN RETURN 30;
WHEN 10 THEN RETURN 30;
WHEN 11 THEN RETURN 30;
WHEN 12 THEN RETURN 29;
END CASE;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `pdate`
-- ----------------------------
DROP FUNCTION IF EXISTS `pdate`;
DELIMITER ;;
CREATE FUNCTION `pdate`(`gdate` datetime) RETURNS char(100) CHARSET utf8
BEGIN
DECLARE
i,
gy, gm, gd,
g_day_no, j_day_no, j_np,
jy, jm, jd INT DEFAULT 0; /* Can be unsigned int? */
DECLARE resout char(100);
DECLARE ttime CHAR(20);
SET gy = YEAR(gdate) - 1600;
SET gm = MONTH(gdate) - 1;
SET gd = DAY(gdate) - 1;
SET ttime = TIME(gdate);
SET g_day_no = ((365 * gy) + __mydiv(gy + 3, 4) - __mydiv(gy + 99, 100) + __mydiv (gy + 399, 400));
SET i = 0;
WHILE (i < gm) do
SET g_day_no = g_day_no + _gdmarray(i);
SET i = i + 1;
END WHILE;
IF gm > 1 and ((gy % 4 = 0 and gy % 100 <> 0)) or gy % 400 = 0 THEN
SET g_day_no = g_day_no + 1;
END IF;
SET g_day_no = g_day_no + gd;
SET j_day_no = g_day_no - 79;
SET j_np = j_day_no DIV 12053;
SET j_day_no = j_day_no % 12053;
SET jy = 979 + 33 * j_np + 4 * __mydiv(j_day_no, 1461);
SET j_day_no = j_day_no % 1461;
IF j_day_no >= 366 then
SET jy = jy + __mydiv(j_day_no - 1, 365);
SET j_day_no = (j_day_no - 1) % 365;
END IF;
SET i = 0;
WHILE (i < 11 and j_day_no >= _jdmarray(i)) do
SET j_day_no = j_day_no - _jdmarray(i);
SET i = i + 1;
END WHILE;
SET jm = i + 1;
SET jd = j_day_no + 1;
SET resout = CONCAT_WS ('-', jy, jm, jd);
IF (ttime <> '00:00:00') then
SET resout = CONCAT_WS(' ', resout, ttime);
END IF;
RETURN resout;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `PMONTH`
-- ----------------------------
DROP FUNCTION IF EXISTS `PMONTH`;
DELIMITER ;;
CREATE FUNCTION `PMONTH`(`gdate` datetime) RETURNS char(100) CHARSET utf8
BEGIN
DECLARE
i,
gy, gm, gd,
g_day_no, j_day_no, j_np,
jy, jm, jd INT DEFAULT 0; /* Can be unsigned int? */
DECLARE resout char(100);
DECLARE ttime CHAR(20);
SET gy = YEAR(gdate) - 1600;
SET gm = MONTH(gdate) - 1;
SET gd = DAY(gdate) - 1;
SET ttime = TIME(gdate);
SET g_day_no = ((365 * gy) + __mydiv(gy + 3, 4) - __mydiv(gy + 99, 100) + __mydiv(gy + 399, 400));
SET i = 0;
WHILE (i < gm) do
SET g_day_no = g_day_no + _gdmarray(i);
SET i = i + 1;
END WHILE;
IF gm > 1 and ((gy % 4 = 0 and gy % 100 <> 0)) or gy % 400 = 0 THEN
SET g_day_no = g_day_no + 1;
END IF;
SET g_day_no = g_day_no + gd;
SET j_day_no = g_day_no - 79;
SET j_np = j_day_no DIV 12053;
set j_day_no = j_day_no % 12053;
SET jy = 979 + 33 * j_np + 4 * __mydiv(j_day_no, 1461);
SET j_day_no = j_day_no % 1461;
IF j_day_no >= 366 then
SET jy = jy + __mydiv(j_day_no - 1, 365);
SET j_day_no =(j_day_no - 1) % 365;
END IF;
SET i = 0;
WHILE (i < 11 and j_day_no >= _jdmarray(i)) do
SET j_day_no = j_day_no - _jdmarray(i);
SET i = i + 1;
END WHILE;
SET jm = i + 1;
SET jd = j_day_no + 1;
RETURN jm;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `pmonthname`
-- ----------------------------
DROP FUNCTION IF EXISTS `pmonthname`;
DELIMITER ;;
CREATE FUNCTION `pmonthname`(`gdate` datetime) RETURNS varchar(100) CHARSET utf8
BEGIN
CASE PMONTH(gdate)
WHEN 1 THEN RETURN 'ÝÑæÑÏíä';
WHEN 2 THEN RETURN 'ÇÑÏíÈåÔÊ';
WHEN 3 THEN RETURN 'ÎÑÏÇÏ';
WHEN 4 THEN RETURN '撄';
WHEN 5 THEN RETURN 'ãÑÏÇÏ';
WHEN 6 THEN RETURN 'ÔåÑíæÑ';
WHEN 7 THEN RETURN 'ãåÑ';
WHEN 8 THEN RETURN 'ÂÈÇä';
WHEN 9 THEN RETURN 'ÂÐÑ';
WHEN 10 THEN RETURN 'Ïí';
WHEN 11 THEN RETURN 'Èåãä';
WHEN 12 THEN RETURN 'ÇÓÝäÏ';
END CASE;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `pyear`
-- ----------------------------
DROP FUNCTION IF EXISTS `pyear`;
DELIMITER ;;
CREATE FUNCTION `pyear`(`gdate` datetime) RETURNS char(100) CHARSET utf8
BEGIN
DECLARE
i,
gy, gm, gd,
g_day_no, j_day_no, j_np,
jy, jm, jd INT DEFAULT 0; /* Can be unsigned int? */
DECLARE resout char(100);
DECLARE ttime CHAR(20);
SET gy = YEAR(gdate) - 1600;
SET gm = MONTH(gdate) - 1;
SET gd = DAY(gdate) - 1;
SET ttime = TIME(gdate);
SET g_day_no = ((365 * gy) + __mydiv(gy + 3, 4) - __mydiv(gy + 99, 100) + __mydiv(gy + 399, 400));
SET i = 0;
WHILE (i < gm) do
SET g_day_no = g_day_no + _gdmarray(i);
SET i = i + 1;
END WHILE;
IF gm > 1 and ((gy % 4 = 0 and gy % 100 <> 0)) or gy % 400 = 0 THEN
SET g_day_no = g_day_no + 1;
END IF;
SET g_day_no = g_day_no + gd;
SET j_day_no = g_day_no - 79;
SET j_np = j_day_no DIV 12053;
set j_day_no = j_day_no % 12053;
SET jy = 979 + 33 * j_np + 4 * __mydiv(j_day_no, 1461);
SET j_day_no = j_day_no % 1461;
IF j_day_no >= 366 then
SET jy = jy + __mydiv(j_day_no - 1, 365);
SET j_day_no = (j_day_no - 1) % 365;
END IF;
SET i = 0;
WHILE (i < 11 and j_day_no >= _jdmarray(i)) do
SET j_day_no = j_day_no - _jdmarray(i);
SET i = i + 1;
END WHILE;
SET jm = i + 1;
SET jd = j_day_no + 1;
RETURN jy;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `pday`
-- ----------------------------
DROP FUNCTION IF EXISTS `pday`;
DELIMITER ;;
CREATE FUNCTION `pday`(`gdate` datetime) RETURNS char(100) CHARSET utf8
BEGIN
DECLARE
i,
gy, gm, gd,
g_day_no, j_day_no, j_np,
jy, jm, jd INT DEFAULT 0; /* Can be unsigned int? */
DECLARE resout char(100);
DECLARE ttime CHAR(20);
SET gy = YEAR(gdate) - 1600;
SET gm = MONTH(gdate) - 1;
SET gd = DAY(gdate) - 1;
SET ttime = TIME(gdate);
SET g_day_no = ((365 * gy) + __mydiv(gy + 3, 4) - __mydiv(gy + 99 , 100) + __mydiv(gy + 399, 400));
SET i = 0;
WHILE (i < gm) do
SET g_day_no = g_day_no + _gdmarray(i);
SET i = i + 1;
END WHILE;
IF gm > 1 and ((gy % 4 = 0 and gy % 100 <> 0)) or gy % 400 = 0 THEN
SET g_day_no = g_day_no + 1;
END IF;
SET g_day_no = g_day_no + gd;
SET j_day_no = g_day_no - 79;
SET j_np = j_day_no DIV 12053;
SET j_day_no = j_day_no % 12053;
SET jy = 979 + 33 * j_np + 4 * __mydiv(j_day_no, 1461);
SET j_day_no = j_day_no % 1461;
IF j_day_no >= 366 then
SET jy = jy + __mydiv(j_day_no - 1, 365);
SET j_day_no = (j_day_no-1) % 365;
END IF;
SET i = 0;
WHILE (i < 11 and j_day_no >= _jdmarray(i)) do
SET j_day_no = j_day_no - _jdmarray(i);
SET i = i + 1;
END WHILE;
SET jm = i + 1;
SET jd = j_day_no + 1;
RETURN jd;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `_gdmarray2`
-- ----------------------------
DROP FUNCTION IF EXISTS `_gdmarray2`;
DELIMITER ;;
CREATE FUNCTION `_gdmarray2`(`m` smallint, `k` SMALLINT) RETURNS smallint(2)
BEGIN
CASE m
WHEN 0 THEN RETURN 31;
WHEN 1 THEN RETURN 28+k;
WHEN 2 THEN RETURN 31;
WHEN 3 THEN RETURN 30;
WHEN 4 THEN RETURN 31;
WHEN 5 THEN RETURN 30;
WHEN 6 THEN RETURN 31;
WHEN 7 THEN RETURN 31;
WHEN 8 THEN RETURN 30;
WHEN 9 THEN RETURN 31;
WHEN 10 THEN RETURN 30;
WHEN 11 THEN RETURN 31;
END CASE;
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `gdate`
-- ----------------------------
DROP FUNCTION IF EXISTS `gdate`;
DELIMITER ;;
CREATE FUNCTION `gdate`(`jy` smallint, `jm` smallint, `jd` smallint) RETURNS datetime
BEGIN
DECLARE
i, j, e, k, mo,
gy, gm, gd,
g_day_no, j_day_no, bkab, jmm, mday, g_day_mo, bkab1, j1
INT DEFAULT 0; /* Can be unsigned int? */
DECLARE resout char(100);
DECLARE fdate datetime;
SET bkab = __mymod(jy,33);
IF (bkab = 1 or bkab= 5 or bkab = 9 or bkab = 13 or bkab = 17 or bkab = 22 or bkab = 26 or bkab = 30) THEN
SET j=1;
end IF;
SET bkab1 = __mymod(jy+1,33);
IF (bkab1 = 1 or bkab1= 5 or bkab1 = 9 or bkab1 = 13 or bkab1 = 17 or bkab1 = 22 or bkab1 = 26 or bkab1 = 30) THEN
SET j1=1;
end IF;
CASE jm
WHEN 1 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 2 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 3 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 4 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 5 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 6 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 7 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 8 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 9 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 10 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 11 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 12 THEN IF jd > _jdmarray2(jm)+j or jd <= 0 THEN SET e=1; end IF;
END CASE;
IF jm > 12 or jm <= 0 THEN SET e=1; end IF;
IF jy <= 0 THEN SET e=1; end IF;
IF e>0 THEN
RETURN 0;
end IF;
IF (jm>=11) or (jm=10 and jd>=11 and j=0) or (jm=10 and jd>11 and j=1) THEN
SET i=1;
end IF;
SET gy = jy + 621 + i;
IF (__mymod(gy,4)=0) THEN
SET k=1;
end IF;
IF (__mymod(gy,100)=0) and (__mymod(gy,400)<>0) THEN
SET k=0;
END IF;
SET jmm=jm-1;
WHILE (jmm > 0) do
SET mday=mday+_jdmarray2(jmm);
SET jmm=jmm-1;
end WHILE;
SET j_day_no=(jy-1)*365+(__mydiv(jy,4))+mday+jd;
SET g_day_no=j_day_no+226899;
SET g_day_no=g_day_no-(__mydiv(gy-1,4));
SET g_day_mo=__mymod(g_day_no,365);
IF (k=1 and j=1) THEN
IF (g_day_mo=0) THEN
RETURN CONCAT_WS('-',gy,'12','30');
END IF;
IF (g_day_mo=1) THEN
RETURN CONCAT_WS('-',gy,'12','31');
END IF;
END IF;
IF (g_day_mo=0) THEN
RETURN CONCAT_WS('-',gy,'12','31');
END IF;
SET mo=0;
SET gm=gm+1;
while g_day_mo>_gdmarray2(mo,k) do
SET g_day_mo=g_day_mo-_gdmarray2(mo,k);
SET mo=mo+1;
SET gm=gm+1;
end WHILE;
SET gd=g_day_mo;
RETURN CONCAT_WS('-',gy,gm,gd);
END;;
DELIMITER ;
-- ----------------------------
-- Function structure for `gdatestr`
-- ----------------------------
DROP FUNCTION IF EXISTS `gdatestr`;
DELIMITER ;;
CREATE FUNCTION `gdatestr`(`jdat` char(10)) RETURNS datetime
BEGIN
DECLARE
i, j, e, k, mo,
gy, gm, gd,
g_day_no, j_day_no, bkab, jmm, mday, g_day_mo, jd, jy, jm,bkab1,j1
INT DEFAULT 0; /* ### Can't be unsigned int! ### */
DECLARE resout char(100);
DECLARE jdd, jyd, jmd, jt varchar(100);
DECLARE fdate datetime;
SET jdd = SUBSTRING_INDEX(jdat, '/', -1);
SET jt = SUBSTRING_INDEX(jdat, '/', 2);
SET jyd = SUBSTRING_INDEX(jt, '/', 1);
SET jmd = SUBSTRING_INDEX(jt, '/', -1);
SET jd = CAST(jdd as SIGNED);
SET jy = CAST(jyd as SIGNED);
SET jm = CAST(jmd as SIGNED);
SET bkab = __mymod(jy,33);
IF (bkab = 1 or bkab= 5 or bkab = 9 or bkab = 13 or bkab = 17 or bkab = 22 or bkab = 26 or bkab = 30) THEN
SET j=1;
end IF;
SET bkab1 = __mymod(jy+1,33);
IF (bkab1 = 1 or bkab1= 5 or bkab1 = 9 or bkab1 = 13 or bkab1 = 17 or bkab1 = 22 or bkab1 = 26 or bkab1 = 30) THEN
SET j1=1;
end IF;
CASE jm
WHEN 1 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 2 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 3 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 4 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 5 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 6 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 7 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 8 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 9 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 10 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 11 THEN IF jd > _jdmarray2(jm) or jd <= 0 THEN SET e=1; end IF;
WHEN 12 THEN IF jd > _jdmarray2(jm)+j or jd <= 0 THEN SET e=1; end IF;
END CASE;
IF jm > 12 or jm <= 0 THEN SET e=1; end IF;
IF jy <= 0 THEN SET e=1; end IF;
IF e>0 THEN
RETURN 0;
end IF;
IF (jm>=11) or (jm=10 and jd>=11 and j=0) or (jm=10 and jd>11 and j=1) THEN
SET i=1;
end IF;
SET gy = jy + 621 + i;
IF (__mymod(gy,4)=0) THEN
SET k=1;
end IF;
IF (__mymod(gy,100)=0) and (__mymod(gy,400)<>0) THEN
SET k=0;
END IF;
SET jmm=jm-1;
WHILE (jmm > 0) do
SET mday=mday+_jdmarray2(jmm);
SET jmm=jmm-1;
end WHILE;
SET j_day_no=(jy-1)*365+(__mydiv(jy,4))+mday+jd;
SET g_day_no=j_day_no+226899;
SET g_day_no=g_day_no-(__mydiv(gy-1,4));
SET g_day_mo=__mymod(g_day_no,365);
IF (k=1 and j=1) THEN
IF (g_day_mo=0) THEN
RETURN CONCAT_WS('-',gy,'12','30');
END IF;
IF (g_day_mo=1) THEN
RETURN CONCAT_WS('-',gy,'12','31');
END IF;
END IF;
IF (g_day_mo=0) THEN
RETURN CONCAT_WS('-',gy,'12','31');
END IF;
SET mo=0;
SET gm=gm+1;
while g_day_mo>_gdmarray2(mo,k) do
SET g_day_mo=g_day_mo-_gdmarray2(mo,k);
SET mo=mo+1;
SET gm=gm+1;
end WHILE;
SET gd=g_day_mo;
RETURN CONCAT_WS('-',gy,gm,gd);
END;;
DELIMITER ;
Then I receive the error:
#1227 - Access denied; you need the SUPER privilege for this operation
I've changed all database command such as select delete alter, ... to yes into my MySQL database manager. I am using directAdmin v1.41.1.

I would try to GRANT permissions to that user ID and client IP and see if that helps.
http://dev.mysql.com/doc/refman/5.0/en/grant.html

Related

MySQL - Matching Two Tables

I have two tables, first called list_city contain list of city in Europe, example:
=========================
= ID = CITY = COUNTRY =
=========================
= 1 = LONDON = UK =
= 2 = PARIS = France =
= 3 = ROME = Italy =
=========================
and the second one is data_customer, contain id_cust and city (but in mess typing)
====================
= ID_CUST = CITY =
====================
= 012AGH = paris =
= 2X4BV = London =
= M3RT45 = romE =
= 1F546 = Lndon =
= 345GC = PArs =
= 54A78 = roma =
====================
How I can matching in the city in the second table with the city in the first table in mysql so the output will be:
====================
= ID_CUST = CITY =
====================
= 012AGH = PARIS =
= 2X4BV = LONDON =
= M3RT45 = ROME =
= 1F546 = LONDON =
= 345GC = PARIS =
= 54A78 = ROME =
====================
I tried
SELECT c1.ID_CUST, c2.CITY FROM data_customer c1 INNER JOIN list_city c2
ON c1.CITY LIKE concat('%',c2.CITY,'%');
But, It's seem not work as Iexpected. It's possible do it only in mysql? If not can anyone give me another solution? Thank you very much I really appreciate your time to help me.
Use Levenshtein distance.
SELECT DISTINCT
data_customer.id_cust,
FIRST_VALUE(list_city.city) OVER ( PARTITION BY data_customer.id_cust
ORDER BY levenshtein(UPPER(list_city.city), UPPER(data_customer.city))) city
FROM list_city
CROSS JOIN data_customer;
Of course you must create user-defined function:
CREATE FUNCTION levenshtein( s1 VARCHAR(255), s2 VARCHAR(255) )
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
DECLARE s1_char CHAR;
DECLARE cv0, cv1 VARBINARY(256);
SET s1_len = CHAR_LENGTH(s1),
s2_len = CHAR_LENGTH(s2),
cv1 = 0x00,
j = 1,
i = 1,
c = 0;
IF s1 = s2 THEN
RETURN 0;
ELSEIF s1_len = 0 THEN
RETURN s2_len;
ELSEIF s2_len = 0 THEN
RETURN s1_len;
ELSE
WHILE j <= s2_len DO
SET cv1 = CONCAT(cv1, UNHEX(HEX(j))),
j = j + 1;
END WHILE;
WHILE i <= s1_len DO
SET s1_char = SUBSTRING(s1, i, 1),
c = i,
cv0 = UNHEX(HEX(i)),
j = 1;
WHILE j <= s2_len DO
SET c = c + 1;
IF s1_char = SUBSTRING(s2, j, 1) THEN
SET cost = 0;
ELSE
SET cost = 1;
END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
IF c > c_temp THEN
SET c = c_temp;
END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
IF c > c_temp THEN
SET c = c_temp;
END IF;
SET cv0 = CONCAT(cv0, UNHEX(HEX(c))),
j = j + 1;
END WHILE;
SET cv1 = cv0,
i = i + 1;
END WHILE;
END IF;
RETURN c;
END;
fiddle

How to two used while Syntax in Mysql Procecure

I would like to write two While Syntax.
The results I expect are as follows.
when 'H'
tagName1, tagName2, tagname3
when 'D'
tagName1, tagName2, tagname3
when 'M'
tagName1, tagName2, tagname3
when 'Y'
tagName1, tagName2, tagname3
But it didn't worked....
Below is the code that I wrote, while I play it only once.
Any advice, please..
DECLARE tagList varchar(255) DEFAULT 'tagName1,tagName2,tagName3';
DECLARE tagTypeList varchar(150) DEFAULT 'H,D,M,Y';
WHILE tagTypeList != '' DO
WHILE tagList != '' DO
SET tagNameArray = SUBSTRING_INDEX(tagList, ',', 1);
SET tagTypeArray = SUBSTRING_INDEX(tagTypeList, ',', 1);
IF(tagTypeArray = 'H') THEN
SET aDate = (SELECT (DATE_ADD(nDateTime, INTERVAL -1 HOUR)));
SET ago_Y = (SELECT DATE_FORMAT(aDate,'%Y'));
SET ago_M = (SELECT DATE_FORMAT(aDate,'%m'));
SET ago_D = (SELECT DATE_FORMAT(aDate,'%d'));
SET ago_H = (SELECT DATE_FORMAT(aDate,'%H'));
SET ago_W = (SELECT DATE_FORMAT(aDate,'%w'));
ELSEIF(tagTypeArray = 'D') THEN
SET aDate = (SELECT (DATE_ADD(nDateTime, INTERVAL -1 DAY)));
SET ago_Y = (SELECT DATE_FORMAT(aDate,'%Y'));
SET ago_M = (SELECT DATE_FORMAT(aDate,'%m'));
SET ago_D = (SELECT DATE_FORMAT(aDate,'%d'));
SET ago_H = 0;
SET ago_W = 0;
ELSEIF(tagTypeArray = 'M') THEN
SET aDate = (SELECT (DATE_ADD(nDateTime, INTERVAL -1 MONTH)));
SET ago_Y = (SELECT DATE_FORMAT(aDate,'%Y'));
SET ago_M = (SELECT DATE_FORMAT(aDate,'%m'));
SET ago_D = 0;
SET ago_H = 0;
SET ago_W = 0;
ELSEIF(tagTypeArray = 'Y') THEN
SET aDate = (SELECT (DATE_ADD(nDateTime, INTERVAL -1 YEAR)));
SET ago_Y = (SELECT DATE_FORMAT(aDate,'%Y'));
SET ago_M = 0;
SET ago_D = 0;
SET ago_H = 0;
SET ago_W = 0;
END IF;
SET selectValue = (SELECT tagvalue FROM datasource WHERE tagname = tagNameArray and tagtype = tagTypeArray and y=ago_Y and m=ago_M and d=ago_D and h=ago_H);
IF(selectValue IS NULL OR selectValue = '')
THEN
SET old_Value_3M = (Select LastValue from BwAnalogTable where TagName = tagNameArray and LogDate >= ago_3M order by LogDate desc, LogTime desc limit 1);
# Here Insert Querty
END IF;
IF LOCATE(',', tagList) > 0 THEN
SET tagList = SUBSTRING(tagList, LOCATE(',', tagList) + 1);
ELSE
SET tagList = '';
END IF;
END WHILE;
IF LOCATE(',', tagTypeList) > 0 THEN
SET tagTypeList = SUBSTRING(tagTypeList, LOCATE(',', tagTypeList) + 1);
ELSE
SET tagTypeList = '';
END IF;
END WHILE;
I have modified the code as shown below.
DECLARE done INT DEFAULT FALSE;
DECLARE tagType2 VARCHAR(255) DEFAULT '';
DECLARE tagName2 VARCHAR(255) DEFAULT '';
DECLARE tagType_Cur CURSOR for select tagType from tbtagtype;
DECLARE tagName_Cur CURSOR for select tagName from tbtaglist;
OPEN tagType_Cur;
LOOP1: LOOP
FETCH tagType_Cur INTO tagType2;
IF done THEN
CLOSE tagType_Cur;
LEAVE LOOP1;
END IF;
########## here insert your core query ###############
OPEN tagName_Cur;
LOOP2: LOOP
FETCH tagName_Cur INTO tagName2;
IF done THEN
CLOSE tagName_Cur;
SET done = FALSE;
LEAVE LOOP2;
END IF;
########## here insert your core query ###############
END LOOP LOOP2;
END LOOP LOOP1;

Smart Search MySQL

I'm not an SQL Expert but I want to know the query that searches at the same time passes these rules
# they differ only by one symbol;
# user's input has one extra symbol;
# user's input has one missing symbol.
# Note that the comparison should be case-insensitive.
Something like:
SELECT `name` FROM `table` WHERE `name` = 'frd'
| name |
--------
ford <---- # user's input has one missing symbol.
crd <----- # they differ only by one symbol
fr <------ # user's input has one extra symbol;
FRD <----- # Note that the comparison should be case-insensitive.
You need an implementation of Levenshtein. Like this one: http://www.artfulsoftware.com/infotree/qrytip.php?id=552
DELIMITER //
CREATE FUNCTION levenshtein( s1 VARCHAR(255), s2 VARCHAR(255) )
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
DECLARE s1_char CHAR;
-- max strlen=255
DECLARE cv0, cv1 VARBINARY(256);
SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2), cv1 = 0x00, j = 1, i = 1, c = 0;
IF s1 = s2 THEN
RETURN 0;
ELSEIF s1_len = 0 THEN
RETURN s2_len;
ELSEIF s2_len = 0 THEN
RETURN s1_len;
ELSE
WHILE j <= s2_len DO
SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
END WHILE;
WHILE i <= s1_len DO
SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)), j = 1;
WHILE j <= s2_len DO
SET c = c + 1;
IF s1_char = SUBSTRING(s2, j, 1) THEN
SET cost = 0; ELSE SET cost = 1;
END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
IF c > c_temp THEN SET c = c_temp; END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
IF c > c_temp THEN
SET c = c_temp;
END IF;
SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
END WHILE;
SET cv1 = cv0, i = i + 1;
END WHILE;
END IF;
RETURN c;
END; //
Then you can use it like this:
SELECT * FROM yourTable WHERE levenshtein(yourColumn, 'word') <= 1
DELIMITER IS NEEDED FOR IT TO WORK
DELIMITER //
CREATE FUNCTION levenshtein( s1 VARCHAR(255), s2 VARCHAR(255) )
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
DECLARE s1_char CHAR;
-- max strlen=255
DECLARE cv0, cv1 VARBINARY(256);
SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2), cv1 = 0x00, j = 1, i = 1, c = 0;
IF s1 = s2 THEN
RETURN 0;
ELSEIF s1_len = 0 THEN
RETURN s2_len;
ELSEIF s2_len = 0 THEN
RETURN s1_len;
ELSE
WHILE j <= s2_len DO
SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
END WHILE;
WHILE i <= s1_len DO
SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)), j = 1;
WHILE j <= s2_len DO
SET c = c + 1;
IF s1_char = SUBSTRING(s2, j, 1) THEN
SET cost = 0; ELSE SET cost = 1;
END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
IF c > c_temp THEN SET c = c_temp; END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
IF c > c_temp THEN
SET c = c_temp;
END IF;
SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
END WHILE;
SET cv1 = cv0, i = i + 1;
END WHILE;
END IF;
RETURN c;
END //

Trigger runs without errors but does not work

I created a trigger in mysql, it's no compilation error, but does nothing of what was proposed in it, is not the same as it was called.
Following code.
DELIMITER //
DROP TRIGGER BILHETAGEM_ACERTOS//
CREATE TRIGGER BILHETAGEM_ACERTOS
BEFORE INSERT ON bilhetagem FOR EACH ROW
BEGIN
SET new.bil_canal = LEFT(new.bil_canal,locate('-',new.bil_canal)-1);
SET #plano = 0;
SET #tarifa = 0;
SET #placa = 0;
SET #seq = 0;
SET #canal = 0;
SET #tronco = 0;
SET #troncoplaca = 0;
SET new.bil_gravacao = SUBSTRING(new.bil_gravacao,7);
SELECT SUBSTRING(new.bil_canal, 1, (LOCATE('/', new.bil_canal) -1)) INTO #placa;
IF (#placa = 'Khomp') THEN
SET #canal = RIGHT(new.bil_canal,LENGTH(new.bil_canal) - LOCATE('/', new.bil_canal));
SET #seq = LEFT(#canal, LOCATE('C', #canal) -1);
IF (LOCATE('-', #canal) > 0) THEN
SET #canal = LEFT(#canal, LOCATE('-', #canal) -1);
END IF;
SET #canal = RIGHT(#canal,(LENGTH(#canal) - LOCATE('C', #canal)));
SELECT tro_codigo INTO #tronco FROM troncos_placas where tropla_placa = #seq LIMIT 1;
SELECT tro_placa INTO #tipo FROM troncos where tro_codigo = #tronco LIMIT 1;
SELECT tro_nome INTO #tronco_nome FROM troncos where tro_codigo = #tronco LIMIT 1;
IF (#tipo = 'E1') THEN
SELECT tropla_codigo INTO #troncoplaca FROM troncos_placas where tro_codigo = #tronco LIMIT 1;
ELSE
SELECT tro_codigo INTO #tronco FROM troncos_placas where tropla_placa = #seq and tropla_codigo = #canal LIMIT 1;
SET #troncoplaca = #canal;
END IF;
ELSE
SELECT SUBSTRING(new.bil_canal, (LOCATE('/', new.bil_canal) +1)) INTO #canal;
SELECT tro_codigo INTO #tronco FROM troncos where tro_tipo = #placa and tro_apelido = #canal LIMIT 1;
SET #troncoplaca = 0;
END IF;
IF (LOCATE('B', new.ram_codigo)) THEN
SET new.ram_codigo = #tronco_nome;
END IF;
IF (LOCATE('GATW', new.ram_codigo)) THEN
SET new.ram_codigo = #tronco_nome;
END IF;
SET new.tro_codigo = #tronco;
SET new.tropla_codigo = #troncoplaca;
SELECT pla_codigo INTO #plano FROM ramal where ram_codigo = new.ram_codigo LIMIT 1;
IF #plano > 0 THEN
IF ((LEFT(new.bil_destino,1) > 0) and (LEFT(new.bil_destino,1) <= 5)) THEN
SELECT pla_vlrminutolocalfixo INTO #tarifa FROM planos where pla_codigo = #plano LIMIT 1;
ELSE
IF ((LEFT(new.bil_destino,1) > 5) and (LEFT(new.bil_destino,1) <= 9)) THEN
SELECT pla_vlrminutolocalcelular INTO #tarifa FROM planos where pla_codigo = #plano LIMIT 1;
ELSE
IF (LEFT(new.bil_destino,4) <> '0800') THEN
IF ((SUBSTRING(new.bil_destino,2,1) < 2) and (SUBSTRING(new.bil_destino,4,1) <= 5)) THEN
SELECT pla_vlrminutodddfixo_deestado INTO #tarifa FROM planos where pla_codigo = #plano LIMIT 1;
ELSE
SELECT pla_vlrminutodddfixo_foestado INTO #tarifa FROM planos where pla_codigo = #plano LIMIT 1;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
SET new.pla_codigo = #plano;
SELECT pla_tempo INTO #tempo FROM planos where pla_codigo = #plano LIMIT 1;
SELECT pla_cadencia INTO #cadencia FROM planos where pla_codigo = #plano LIMIT 1;
SET new.pla_tempo = #tempo;
SET new.pla_cadencia = #cadencia;
SET new.bil_tarifabase = #tarifa;
SET new.bil_tarifado = 'S';
SET #relseg = new.bil_duracao;
SET #reldata = DATE(new.bil_dataligacao);
IF ((LENGTH(new.bil_destino) > 4) AND (new.bil_duracao > 0) AND (new.bil_entradasaida <> 'E')) THEN
IF (new.bil_duracao <= #tempo) THEN
SET new.bil_vlrvenda = (#tarifa / 60) * #tempo;
ELSE
SET #valor = (#tarifa / 60) * #cadencia;
SET #tmp = (new.bil_duracao - #tempo);
SET #qtd = 1;
WHILE #tmp > 0 DO
SET #tmp = (#tmp - #cadencia);
SET #qtd = #qtd + 1;
END WHILE;
SET new.bil_vlrvenda = #tarifa + (#valor * #qtd);
END IF;
END IF;
IF (#tronco > 0) THEN
SELECT reltro_segundos INTO #segundos from relatorio_troncos where tro_codigo = #tronco and reltro_dataligacao = #reldata limit 1;
IF ((#segundos > 0) or (#segundos is not null)) THEN
UPDATE relatorio_troncos SET reltro_segundos = (#segundos + #relseg) WHERE tro_codigo = #tronco and reltro_dataligacao = new.bil_dataligacao;
ELSE
INSERT INTO relatorio_troncos (tro_codigo, reltro_dataligacao, reltro_segundos) values (#tronco, #reldata, #relseg);
END IF;
END IF;
END IF;
END//
DELIMITER ;``

Error while using user defined functions MySql

Hi please help me to solve this problem thanks in advance
I defined these functions in database
CREATE FUNCTION levenshtein( s1 VARCHAR(255), s2 VARCHAR(255) )
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
DECLARE s1_char CHAR;
-- max strlen=255
DECLARE cv0, cv1 VARBINARY(256);
SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2), cv1 = 0x00, j = 1, i = 1, c = 0;
IF s1 = s2 THEN
RETURN 0;
ELSEIF s1_len = 0 THEN
RETURN s2_len;
ELSEIF s2_len = 0 THEN
RETURN s1_len;
ELSE
WHILE j <= s2_len DO
SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
END WHILE;
WHILE i <= s1_len DO
SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)), j = 1;
WHILE j <= s2_len DO
SET c = c + 1;
IF s1_char = SUBSTRING(s2, j, 1) THEN
SET cost = 0; ELSE SET cost = 1;
END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
IF c > c_temp THEN SET c = c_temp; END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
IF c > c_temp THEN
SET c = c_temp;
END IF;
SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
END WHILE;
SET cv1 = cv0, i = i + 1;
END WHILE;
END IF;
RETURN c;
END;
and helper function
CREATE FUNCTION levenshtein_ratio( s1 VARCHAR(255), s2 VARCHAR(255) )
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, max_len INT;
SET s1_len = LENGTH(s1), s2_len = LENGTH(s2);
IF s1_len > s2_len THEN
SET max_len = s1_len;
ELSE
SET max_len = s2_len;
END IF;
RETURN ROUND((1 - LEVENSHTEIN(s1, s2) / max_len) * 100);
END;
Now I'm trying to write query using these functions:
My Query:
SELECT cevap
FROM `chat`
WHERE levenshtein_ratio( soru,"Hello" ) >80
LIMIT 1 ;
My chat table has only two columns Varchar(250) "soru" and "cevap"
I get following error when I execute my query