Ahoj, rad bych si napsal proceduru, na vkladani aktualizaci uptime.
Pokud je vkladany cas novejsi, nez prechozi udela update posledniho zaznamu. Pokud je starsi, vytvori novy zaznam. Mam to na logovani uptime. Zatim bych rad pro info vypsal po zavolani procedury informaci.
DROP TABLE IF EXISTS test;
CREATE TABLE test (
id int(11) NOT NULL AUTO_INCREMENT,
uptime bigint NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
DELIMITER $$
DROP PROCEDURE IF EXISTS p_insert_new_uptime $$
CREATE PROCEDURE p_insert_new_uptime (
IN new_uptime LONG
)
COMMENT 'Pokud je nove vkladane cislo vetsi, nez minule, misto INSERT udela UPDATE.'
BEGIN
SELECT id, uptime INTO @old_id, @old_uptime
FROM test
ORDER BY id DESC
LIMIT 1;
IF @old_uptime <= new_uptime THEN
SELECT CONCAT("UPDATE: [old_uptime=", CAST(@old_uptime AS CHAR), "], [new_uptime=", CAST(new_uptime AS CHAR), "]") AS INFO;
UPDATE test SET uptime=new_uptime WHERE id=@old_id;
ELSE
SELECT CONCAT("INSERT: [old_uptime=", CAST(@old_uptime AS CHAR), "], [new_uptime=", CAST(new_uptime AS CHAR), "]") AS INFO;
INSERT INTO test (uptime) VALUES (new_uptime);
END IF;
END$$
Kdyz zavolam:
CALL p_insert_new_uptime(UNIX_TIMESTAMP());
vypise se:
UPDATE: [old_uptime=1459769615], [new_uptime=14597...
pokud ho volam treba po 10s znova, prepise puvodni radek, coz je spravne. Jenze i uplne prvni vlozeni do prazdne tabulky vypise UPDATE: [...
Pokud zavolam:
CALL p_insert_new_uptime(123);
vlozi se novy radek, ale stale se vypise UPDATE: [...
Muzete mi prosim poradit, co delam spatne?
Pouzivam MySQL 5.5.47 na Debianu 8.