0

I aim to have the actions as folows: When a user clicks on one "event", the server will insert a new entry into a database with that eventId and userId in table A. The trigger then comes into play and would then activate upon that insert statement, look at the new rows column eventId, and then search another table, B, to match that eventId and increment on column numberClicks in that entry for table B.

I was searching sources but none gave insight into creating a trigger that would read in column from an insert row entry, search another table for that id and update it.

Is there a straightforward way to allow for this trigger?

1 Answer 1

1

This would be failry simple something as

delimiter //
  create trigger number_clicks after insert on event
  for each row
    begin
    declare click_count int default 0 ;
    select count(*) 
    into click_count
    from clicks 
    where eventId = new.eventId ;

    if click_count > 0 then  
       update clicks set numberClicks = numberClicks + 1 where eventId = new.eventId ;
    else
       insert into clicks (eventId , numberClicks)  values (new.eventId,1);
    end if;
    end ;//  
delimiter ;

So the idea is first check in the click table if there is an entry for the last inserted event and if its there then increment the count by one else add a new entry in the table

Here are some tests

mysql> create table event (eventId int,userId int);
Query OK, 0 rows affected (0.06 sec)

mysql> create table clicks(eventId int , numberClicks int);
Query OK, 0 rows affected (0.08 sec)

mysql> insert into event values (1,1);
Query OK, 1 row affected (0.00 sec)

mysql> select * from clicks ;
+---------+--------------+
| eventId | numberClicks |
+---------+--------------+
|       1 |            1 |
+---------+--------------+
1 row in set (0.00 sec)

mysql> insert into event values (1,2);
Query OK, 1 row affected (0.00 sec)

mysql> select * from clicks ;
+---------+--------------+
| eventId | numberClicks |
+---------+--------------+
|       1 |            2 |
+---------+--------------+
1 row in set (0.00 sec)

mysql> insert into event values (2,4);
Query OK, 1 row affected (0.00 sec)

mysql> select * from clicks ;
+---------+--------------+
| eventId | numberClicks |
+---------+--------------+
|       1 |            2 |
|       2 |            1 |
+---------+--------------+
2 rows in set (0.00 sec)

mysql> insert into event values (2,5);
Query OK, 1 row affected (0.00 sec)

mysql> select * from clicks ;
+---------+--------------+
| eventId | numberClicks |
+---------+--------------+
|       1 |            2 |
|       2 |            2 |
+---------+--------------+
2 rows in set (0.00 sec)
Sign up to request clarification or add additional context in comments.

1 Comment

How did you test this? Is there a built in feature to allow for testing?

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.