Avatar of Jim Youmans
Jim Youmans
Flag for United States of America

asked on 

MySQL 5.6 trigger

I have a table that captures a bunch of information whenever a user logs into my app.  Three of the main fields are username, ip, and datetime.  I created a new table (user_ip_table) which has the following columns.

ID (unique auto gen)

I want to create a trigger on the main table to keep track of the last ip address used by a given user.  So when a new row is inserted into the main table the trigger will fire and do one of the following.

if username/ip combo is present in  user_ip_table then do nothing
if username is present but with a different ip then update the ip address and datetime
if username is not present then insert it with ip and datetime

Is there an easy way to do this?  Or do I need to create 3 triggers, one for each case?


* triggerMySQL Server

Avatar of undefined
Last Comment
Jim Youmans

8/22/2022 - Mon