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 7601391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:09:11+00:00 2026-05-30T23:09:11+00:00

So this should be a fairly straight forward trigger, but my MySQL isn’t great,

  • 0

So this should be a fairly straight forward trigger, but my MySQL isn’t great, so it’s undoubtably a failure on my part.
It’s not updating the stats table at all, even though it should be;

DROP TRIGGER countryUpdate;

DELIMITER //

CREATE TRIGGER countryUpdate AFTER INSERT ON stats
FOR EACH ROW BEGIN
    DECLARE NewIP varchar(16);
    DECLARE NewCountry varchar(80);
    SET NewIP = inet_aton(new.vis_ip);
    SET NewCountry = (SELECT country FROM iptocountry WHERE lower_bound <= NewIP AND upper_bound >= NewIP)
    UPDATE stats
        SET Country = NewCountry

END //

DELIMITER;
  • 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-30T23:09:13+00:00Added an answer on May 30, 2026 at 11:09 pm

    Well, first off, your UPDATE—if it works at all—is changing all rows in the stats table, and its doing that for each row inserted. That really doesn’t make much sense. At minimum, you want to add a where clause to only hit the one row you’ve just inserted.

    Apparently, though, that can’t work at all in MySQL, because “a stored function or trigger cannot modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.” (Look under “Restrictions for Stored Functions”)

    So, instead, you need to use a a before insert trigger, and do a SET new.country = NewCountry to fix the row up before its ever inserted.

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

Sidebar

Related Questions

This should be fairly straight forward but is proving not to be. I want
This seems like it should be fairly straight-forward, but I can't see anything obvious.
This should be a fairly straight forward python question, but I'm getting stuck getting
There probably is a fairly simple and straight-forward answer for this, but for some
This is a fairly straight forward question but I'm having a problem finding a
I'm fairly sure there should be an elegant solution to this (in MATLAB), but
This should be a common scenario, but could not find any relevant post yet..
My web app, up until this point, has been fairly straight forward. I have
This should be a fairly straightforward question, but I haven't been able to find
This should be a fairly common question, but I haven't found a straightforward answer

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.