0

I am trying to write a trigger to remove a certain number of rows when the table hits a limit. This is what I have currently:

IF (SELECT COUNT(rowa) FROM tableA ) > 10

THEN

DELETE FROM table WHERE dateinsert IN (
SELECT * FROM (
    SELECT dateinsert FROM tableA ORDER BY dateinsert ASC limit 1
) AS P
)
end if;

pypmyadmin prompt that i have a syntax error.

1 Answer 1

1

You have syntax error in your sql DELETE statement. You cannot use the keyword table. Below is an example.

I have changed table to tableA in the delete statement.

IF (SELECT COUNT(rowa) FROM tableA ) > 10 THEN
  DELETE FROM tableA WHERE dateinsert IN 
   (SELECT dateinsert FROM
    (SELECT dateinsert FROM tableA ORDER BY dateinsert ASC limit 1) AS tmp1);
END IF;
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Tom, thanks. But I encountered the following error: This verison of MariaDB doesnt yet sypport LIMIT, IN/ALL,ANY/SOME subquery
Hi Ken, sorry about that. I have updated the answer.
Hi Tom, still having error at "syntax near end if". Separately, the query and subquery is working well when executed. Let me try to work on it. Thanks again.
There was a missing semicolon at the end of the DELETE statement, just before END IF.
Hi Tom, thanks again. able to insert the trigger. On a side note, it encounter "Can't update table in stored function/trigger because it is already used by statement which invoked this stored function/trigger" when the row hit 10. I assumed that the table cant be updated as it is locked by the trigger. do you have any work around for this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.