MySQL, SQL

Create Function in MySQL

Let’s see an example:

DELIMITER $$
 CREATE FUNCTION `countonline`(`dtstart` DATETIME, `dtend` DATETIME)
    RETURNS BIGINT(20)
    LANGUAGE SQL
    NOT DETERMINISTIC
    READS SQL DATA
    SQL SECURITY INVOKER
    COMMENT 'my comment'
BEGIN
    DECLARE cc BIGINT(20);
    SELECT COUNT(id) INTO cc FROM users WHERE lastseen BETWEEN dtstart AND dtend;
    RETURN cc;
END$$
 
DELIMITER ;