Tengo esta función que me da el siguiente código de error y no sé qué puede estar mal. El delimiter no es porque en mi editor no hace falta ponerlo.
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 'COUNT(*) INTO veces FROM authentication_tokens INNER JOIN users ON auth' at line 8
CREATE FUNCTION fn_maxloginpassbolt() RETURNS VARCHAR(100) DETERMINISTIC BEGIN
DECLARE usuario VARCHAR(50);
DECLARE veces numeric;
DECLARE frase varchar(100);
SELECT users.username INTO usuario, COUNT(*) INTO veces
FROM authentication_tokens INNER JOIN users
ON authentication_tokens.user_id=users.id
WHERE type='login' AND authentication_tokens.created BETWEEN
CONCAT (YEAR(CURDATE()),'01-01') AND
CONCAT (YEAR(CURDATE()),'-',MONTH(CURDATE()),'-',DAYOFMONTH(CURDATE()),
' ',HOUR(NOW()),':',MINUTE(NOW()),':',SECOND(NOW()))
GROUP BY authentication_tokens.user_id
ORDER BY COUNT(*) DESC
LIMIT 1;
SET frase=CONCAT ('EL usuario que más ha usado Passbolt desde el 1 de enero hasta
el dia de hoy durante el año ',YEAR(CUDATE()),'ha sido ',usuario,' con ',
veces,' inicios de sesion');
RETURN frase; END
Un describe
de las tablas me da esto:
mysql> DESCRIBE authentication_tokens;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | char(36) | NO | PRI | NULL | |
| token | char(36) | NO | | NULL | |
| user_id | char(36) | NO | | NULL | |
| active | tinyint(1) | NO | | 1 | |
| created | datetime | NO | | NULL | |
| modified | datetime | NO | | NULL | |
| type | varchar(16) | NO | | NULL | |
| data | text | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
8 rows in set (0,02 sec)
mysql> DESCRIBE users;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | char(36) | NO | PRI | NULL | |
| role_id | char(36) | NO | | NULL | |
| username | varchar(255) | NO | | NULL | |
| active | tinyint(1) | NO | | 0 | |
| deleted | tinyint(1) | NO | MUL | 0 | |
| created | datetime | NO | | NULL | |
| modified | datetime | NO | | NULL | |
+----------+--------------+------+-----+---------+-------+
7 rows in set (0,00 sec)
BETWEEN
. Por favor, cópianos en la pregunta elDESCRIBE
de las tabla en modo texto.SELECT users.username INTO usuario, COUNT(*) INTO veces
: ¿es correcto? Revisando la documentación, la sintaxis pareciera ser algo así comoSELECT users.username, COUNT(*) INTO usuario, veces
.