I got a table that I am allowing identical entries (duplicates, triplecates etc.) but I also got a column that I want everytime that an entry is being made to be updated with the how many times that entry exists.
So I thought to write a trigger, I can already find the duplicate entries by doing
select count(pid) from items group by pid having count(*);
but the thing is that this query returns less columns that the orinal table (cause there are many duplicates)
so there is no 1 to 1 relation between the query and the table so I can use update. How could I modify this to get the desired result
thank you in advanced.
The main problem that you’ll face here is that MySQL will not allow you to modify the
itemstable using anAFTER INSERT ...trigger following a modification to theitemstable itself (think of how this could lead to a circular reference).One solution is to store the counts in a separate table altogether (say
items_pid_info). The primary key of this table would bepidand it is this table that would be updated by the triggers on the main items table. When you need to access thepidCountfor a givenpidsimply join onto this table and you will have up-to-date pid counts for your given pid. Hence:Now create
INSERT,UPDATEandDELETEtriggers on youritemstable to update theitems_pid_infotable:Hope this helps.