MYSQL ERROR #1442 - Delete with a data-based Function of a select

Asked

Viewed 16 times

0

I was trying to remove repeated values from a table, I needed a group by and a limit, I could only think using an extra Function:

DROP FUNCTION IF EXISTS func;

DELIMITER //

CREATE FUNCTION func(usuario_id INT,data VARCHAR(30),limitador INT
) RETURNS int DETERMINISTIC
BEGIN
 DELETE FROM tabela WHERE tabela.id IN (SELECT
          tabela.id
        FROM
          tabela 
        WHERE
          date(tabela.data_solicitacao) = data
          AND tabela.user_id = usuario_id ORDER BY tabela.id DESC )limit limitador;
          RETURN 0;
END 

//

DELIMITER ;

With this Function I could, from a select, run delete in the most recent records with the exception of the first.

Creating a temporary table is now possible to query and change or remove values.

CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (SELECT
  COUNT(*) AS limitador,
  DATE(table.data_solicitacao) AS data_solicitacao,
  table.user_id as usuario_id
FROM
  table
GROUP BY
  DATE(table.data_solicitacao),
  table.user_id
HAVING
  count(*) > 1 limit 0, 2000);
select func(usuario_id,data_solicitacao,limitador-1) from table2;

It is necessary to use the limit 0,2000 if there would be only 500 rows in the temporary table, in my case it was not enough, besides it is necessary to run the create is the call of Function in only one hit.

  • Error 1442 occurs on the call of which command? Could you specify?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.