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
Related
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
So I want to be able to find similar articles based on their titles and I found out about Levenshtein. I'm going to post the function below. So my question is, how can I use this function? I think it is just for finding single words but in my case I need it to find whole sentences in the form of titles. Is levenshtein even the thing I need? My website is developed in php if it's worth mentioning. Maybe there is some php library that you could suggest.
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
It will be incredibly expensive for a large table, but you can self-join the table to find similar titles:
SELECT t2.id, t2.title
FROM yourTable AS t1
JOIN yourTable AS t2 ON t1.id != t2.id AND levenshtein(t1.title, t2.title) < 10
WHERE t1.id = :selected_id
Replace 10 with how close the titles need to be to each other -- lower numbers are closer.
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 //
on running below block in phpmyadmin it shows
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5
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;
just change the DELIMITER
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 > 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 $$
DELIMITER ;
In your line 5, Use BEGINE instead of DO, in line 7, USE END instead of END WHILE
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