select CONCAT(id, SUBSTRING(SUBSTRING(id,1,1)+SUBSTRING(id,2,1)+SUBSTRING(id,3,1)+SUBSTRING(id,4,1)+SUBSTRING(id,5,1)+SUBSTRING(id,6,1),-1,1)) AS ppc from users where id=357;
DELIMITER $$CREATE FUNCTION `calc_ppk`(ID INT) RETURNS int(11)BEGIN DECLARE ppk INT UNSIGNED DEFAULT 0; DECLARE userID INT UNSIGNED DEFAULT ID; WHILE ID > 0 DO SET ppk = ppk + (ID % 10); SET ID = FLOOR(ID / 10); END WHILE; RETURN CAST(CONCAT(userID, ppk % 10) AS UNSIGNED);END$$