Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6713993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:26:40+00:00 2026-05-26T08:26:40+00:00

I got a table that I am allowing identical entries (duplicates, triplecates etc.) but

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:26:40+00:00Added an answer on May 26, 2026 at 8:26 am

    The main problem that you’ll face here is that MySQL will not allow you to modify the items table using an AFTER INSERT ... trigger following a modification to the items table 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 be pid and it is this table that would be updated by the triggers on the main items table. When you need to access the pidCount for a given pid simply join onto this table and you will have up-to-date pid counts for your given pid. Hence:

    create table items_pid_info    
    (pid int unsigned not null primary key,
    pidCount int unsigned not null);
    

    Now create INSERT, UPDATE and DELETE triggers on your items table to update the items_pid_info table:

    DELIMITER &&
    
    CREATE TRIGGER items_pid_count_ins_trg
    AFTER INSERT ON items
    
    FOR EACH ROW BEGIN
    DECLARE c int;
    
    set c := (select count(*) from items im where im.pid = NEW.pid);
    
    insert into items_pid_info values (NEW.pid,c) on duplicate key update pidCount = c;
    
    END&&
    
    
    CREATE TRIGGER items_pid_count_upd_trg
    AFTER UPDATE ON items
    FOR EACH ROW BEGIN
    
    DECLARE c int;
    
    set c := (select count(*) from items im where im.pid = NEW.pid);
    
    insert into items_pid_info values (NEW.pid,c) on duplicate key update pidCount = c;
    END&&
    
    
    CREATE TRIGGER items_pid_count_del_trg
    AFTER DELETE ON items
    FOR EACH ROW BEGIN
    
    DECLARE c int;
    
    set c := (select count(*) from items im where im.pid = OLD.pid);
    
    insert into items_pid_info values (OLD.pid,c) on duplicate key update pidCount = c;
    END&&
    
    DELIMITER ;
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a table that stores the following: JobID ValidationItemID CreatedBy I want to
I've got a table that caches calculated values for certain dates. I want to
I've got a table that includes a column named valid. This has caused a
I've got a table that collects forms submitted from our website, but for some
I got a table that saves executed purchase orders. For a report I want
I've got a table that has a column where the codes are a string
I've got a TABLE that's actually being used for its intended purpose, to display
I've got a table that looks like: Table 1 -> +----+--------+--------+ | id |
I've got a table that displays fine in Chrome, IE8, and IE9. In IE7,
I've got a table that people have been inserting into getting the primary key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.