douli4337 2013-12-06 14:21
浏览 34
已采纳

确定SQL UPDATE是否影响单个JOINed表

According to http://us3.php.net/manual/en/pdostatement.rowcount.php:

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

Using a single query, is it possible to tell if an individual JOIN'd table was affected? For instance, given the following query, how would I know if t1 was affected and if t2 was affected?

$sql ='UPDATE t1 INNER JOIN t2 ON t2.t1_id=t1.id SET t1.foo=:foo, t2.bar=:bar WHERE t2.id=:id';
$stmt = db::db()->prepare($sql);
$stmt->execute(array('foo'=>123,'bar'=>321,'id'=>10));
$rows_t1=$stmt->rowCount();
$rows_t2=$stmt->rowCount();
  • 写回答

2条回答 默认 最新

  • douxian1892 2013-12-06 14:42
    关注

    The UPDATE_TIME column in the information_schema.tables table approximately answers the question "which table was updated". Basic example:

    SELECT UPDATE_TIME
      FROM information_schema.tables
     WHERE TABLE_SCHEMA = 'database' AND TABLE_NAME = 'table'
    

    If you were to run this right after your modifying statements, you could limit to a window of a few seconds to check if a particular table was updated, like:

    SELECT COUNT(*)
      FROM information_schema.tables
     WHERE TABLE_SCHEMA = 'database' AND TABLE_NAME = 'table'
       AND UPDATE_TIME BETWEEN (NOW() - INTERVAL 30 SECOND) AND NOW();           
    

    which returns 1 if that table was updated in the last 30 seconds, or 0 if not.

    I stress this is an approximate answer, because a query other than the one you last executed might have affected that table. If you were to wrap this in a transaction or a lock, then you could use this to actually answer your question: with the cost of write-locking other connections.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路