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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:23:41+00:00 2026-06-18T05:23:41+00:00

error 1093: MySQL can’t specify target table ‘SENTIERO’ for update in FROM clause This

  • 0

error 1093: MySQL can’t specify target table 'SENTIERO' for update in FROM clause

This is my trigger:

CREATE TRIGGER lunghezza_sentiero_datoderivato_INSERT
AFTER INSERT ON SENTIERO_HA_TAPPA
FOR EACH ROW

BEGIN

UPDATE SENTIERO
SET lunghezza= (SELECT SUM(lunghezza)
                FROM TAPPA, SENTIERO as S2, SENTIERO_HA_TAPPA
                WHERE NEW.IDsentiero=S2.IDsentiero
                and SENTIERO_HA_TAPPA.IDtappa=TAPPA.IDtappa);
WHERE IDsentiero IN (SELECT IDsentiero
                     FROM TAPPA, SENTIERO, SENTIERO_HA_TAPPA
                     WHERE SENTIERO_HA_TAPPA.IDsentiero=SENTIERO.IDsentiero
                     and NEW.IDtappa=SENTIERO_HA_TAPPA.IDtappa);
END$$

I’ve found this article about this issue, check it on
http://verysimple.com/2011/03/30/mysql-cant-specify-target-table-for-update-in-from-clause/

Is it the only chance i have? Please help me

EDIT:: i’ve just added the ‘WHERE’ clause but it signals me “error syntax”… why?

  • 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-06-18T05:23:42+00:00Added an answer on June 18, 2026 at 5:23 am

    Try this instead:

    UPDATE SENTIERO s
    INNER JOIN
    (
       SELECT IDsentiero, TheSum
       FROM
       (
          SELECT S2.IDsentiero, SUM(s2.lunghezza) AS TheSum
          FROM SENTIERO AS S2 
          INNER JOIN SENTIERO_HA_TAPPA AS st ON st.IDsentiero = S2.IDsentiero
          INNER JOIN TAPPA             AS  t ON st.IDtappa    = t.IDtappa
          GROUP BY S2.IDsentiero
       ) AS Sub
    ) AS s2 ON s.IDsentiero  = s2.IDsentiero 
    SET s.lunghezza = s2.TheSum
    WHERE S2.IDsentiero = NEW.IDsentiero;
    

    Update:

    You have to get the sum of the values of lunghezza from the TAPPA table not from the SENTIERO, thats why you were getting NULL values. So the final CREATE TRIGGER code should be like this:

    CREATE TRIGGER lunghezza_sentiero_datoderivato_INSERT
    AFTER INSERT ON SENTIERO_HA_TAPPA
    FOR EACH ROW
    
    BEGIN
    
      UPDATE SENTIERO s
      INNER JOIN
      (
         SELECT IDsentiero, TheSum
         FROM
         (
            SELECT S2.IDsentiero, SUM(t.lunghezza) AS TheSum
            FROM SENTIERO AS S2 
            INNER JOIN SENTIERO_HA_TAPPA AS st ON st.IDsentiero = S2.IDsentiero
            INNER JOIN TAPPA             AS  t ON st.IDtappa    = t.IDtappa
            GROUP BY S2.IDsentiero
         ) AS Sub
      ) AS s2 ON s.IDsentiero  = s2.IDsentiero 
      SET s.lunghezza = s2.TheSum;
    
    END
    

    SQL Fiddle Demo

    Note that: This trigger will update the values of lunghezza in the table SENTIERO, when any row being inserted into the table SENTIERO_HA_TAPPA for all the IDsentiero in the SENTIERO, not just the value of the new inserted IDsentiero.

    To update only the value of lunghezza for the new inserted value of IDsentiero into the table SENTIERO_HA_TAPPA only, add a WHERE S2.IDsentiero = NEW.IDsentiero to the UPDATE statement of the trigger. Like this:

    CREATE TRIGGER lunghezza_sentiero_datoderivato_INSERT
    AFTER INSERT ON SENTIERO_HA_TAPPA
    FOR EACH ROW
    
    BEGIN
    
      UPDATE SENTIERO s
      INNER JOIN
      (
         SELECT IDsentiero, TheSum
         FROM
         (
            SELECT S2.IDsentiero, SUM(t.lunghezza) AS TheSum
            FROM SENTIERO AS S2 
            INNER JOIN SENTIERO_HA_TAPPA AS st ON st.IDsentiero = S2.IDsentiero
            INNER JOIN TAPPA             AS  t ON st.IDtappa    = t.IDtappa
            GROUP BY S2.IDsentiero
         ) AS Sub
      ) AS s2 ON s.IDsentiero  = s2.IDsentiero 
      SET s.lunghezza = s2.TheSum
      WHERE S2.IDsentiero = NEW.IDsentiero;
    
    END;
    

    For instance, if you create the three tables, then insert the data to the tables and then create that trigger. Then do an insert into the table SENTIERO_HA_TAPPA like this:

    INSERT INTO `SENTIERO_HA_TAPPA` (`IDtappa`, `IDsentiero`) VALUES (14, 4); 
    

    Then the trigger will update the value of the IDsentiero = 4 only in the table SENTIERO, not all the values of it. And the values of lunghezza for other IDsentiero‘s will be NULLs:

    • Updated SQL Fiddle Demo.

    So, you have to create your tables and the triggers before any insert. Then do the insertions into the tables, so that you get a consistent data. That’s how it should work.

    Like in the following demo:

    • SQL Fiddle Demo.

    Note that: In all the demos in this answer, I used only the three tables involved with the trigger, also I modified the two fields inizio, fine to be nullable in the table TAPPA, because your insert clauses into that table have NULL values to those columns.

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

Sidebar

Related Questions

Possible Duplicate: Mysql error 1093 - Can’t specify target table for update in FROM
How to fix this error [Err] 1093 - You can't specify target table 'user_log'
Possible Duplicate: SQL Delete: can't specify target table for update in FROM clause I
Below query gives me #1093 - You can't specify target table 'reservation_seats' for update
Error 1093 states that you can't UPDATE or DELETE using a subquery if your
I'm doing it this way: UPDATE products SET products_image = (SELECT products_image FROM products
In this table, there is a column called 'position'. This column counts up from
On MySQL, this does not run: delete from robottinosino where date = (select max(date)
This has me stumped. MySQL UPDATE sets SET sets.current_count = (SELECT COUNT(leads_auto.set_id) AS current_count
I can not build a query to mysql, to remove the lines from 200

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.