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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度