dqhsv0147421 2011-12-15 10:20
浏览 38

如何使用具有动态值的变量更新sql表

I have a php code which sums up the value of the same column from two different tables of same database and stores it in a variable. The code is mentioned below:

$sql = 'SELECT
        (SELECT SUM( time_spent )
         FROM '.TICKET_RESPONSE_TABLE.'
         WHERE ticket_id='.db_input($id).')
        +(SELECT SUM( time_spent )
          FROM '.TICKET_NOTE_TABLE.'
          WHERE ticket_id='.db_input($id).')
    AS total_time';
$result = db_query($sql);
$cursor = mysql_fetch_row($result);
$total_time = $cursor[0];

Now,I want is to update a column in another table of the same database, with the value stored in the variable $total_time. Kindly help me with the same.

  • 写回答

3条回答 默认 最新

  • douwen0612 2011-12-15 10:23
    关注

    You can make a subselect:

    UPDATE table1 t1
    SET t1.val1 =
    (SELECT val FROM table2 t2 WHERE t2.id = t1.t2_id )
    WHERE t1.val1 = '';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?