duanao2688 2016-08-16 06:33
浏览 118
已采纳

错误代码:1093。您无法在FROM子句中为更新指定目标表'mm'

i google this question several time but failed to fix mentioned issue. please help me.

My query:

UPDATE meter As mm SET mm.mdi_reset_date='2', mm.write_mdi_reset_date='1'
            WHERE mm.msn IN (SELECT m.msn
            FROM meter m
                INNER JOIN connection con ON con.feeder_id = m.feeder_id
                INNER JOIN consumers co ON co.consumer_id = con.customer_id 
                INNER JOIN customer cs ON co.parent_id = cs.customer_id 
                INNER JOIN cs_group_details cd ON cd.customer_id=cs.customer_id 
                WHERE co.batch_no = '30'
            AND cd.cs_group_id='4');
  • 写回答

1条回答 默认 最新

  • dongqiang2358 2016-08-16 06:48
    关注

    First Create temporary table using query

    CREATE TABLE meter_temp LIKE meter;
    

    Now insert data from meter to temporary table meter_temp

    INSERT INTO meter_temp SELECT * FROM meter;
    

    Use your update query

    UPDATE meter As mm SET mm.mdi_reset_date='2', mm.write_mdi_reset_date='1'
                WHERE mm.msn IN (SELECT m.msn
                FROM meter_temp m
                    INNER JOIN connection con ON con.feeder_id = m.feeder_id
                    INNER JOIN consumers co ON co.consumer_id = con.customer_id 
                    INNER JOIN customer cs ON co.parent_id = cs.customer_id 
                    INNER JOIN cs_group_details cd ON cd.customer_id=cs.customer_id 
                    WHERE co.batch_no = '30'
                AND cd.cs_group_id='4');
    

    Let me know if it is working or not.

    Edited ::

    $subQuery = "SELECT m.msn FROM meter_temp m INNER JOIN connection con ON con.feeder_id = m.feeder_id INNER JOIN consumers co ON co.consumer_id = con.customer_id INNER JOIN customer cs ON co.parent_id = cs.customer_id INNER JOIN cs_group_details cd ON cd.customer_id=cs.customer_id WHERE co.batch_no = '30' AND cd.cs_group_id='4'";
    $result = mysqli($con,$query); // assuming you are using mysqli. change as per your code
    while($row = mysqli_fetch_assoc($result))
    {
       $msnArray[] = $row['msn'];
    }
    $msn = implode(",",$msnArray); // assuming msn is integer and not string.
    // if it is string then you have to wrap it in quotes.
    // Now run update query
    $update = "UPDATE meter SET mdi_reset_date='2', write_mdi_reset_date='1' WHERE msn IN ({$msn})";
    $updateResult = mysqli_query($con,$update);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀