Mysql Update Query运行两次

时间:2014-03-07 13:13:39

标签: php mysql codeigniter pdo

下面的查询在mysql终端,工作台和php中执行不同的方法。 mysql终端和工作台给出了相同的结果,但是php给出了不同的结果(好像查询执行了2次)。 我想从数量减少1项。但是在php( codeigniter,pdo connection )中,它会从数量中减去2个项目(总是我提到的两倍)。 注意:我很确定我没有执行此查询两次(在循环中)。

UPDATE tbl_stock tsk,
    (SELECT 
        tsk.id_stock, tsk.qty
    FROM
        tbl_store ts
    inner join tbl_stock tsk ON ts.id_store = tsk.id_store
    where
        ts.id_physical_place = 2
            and ts.store_status = 1
            and tsk.stock_status = 1
            and tsk.id_products = 796
    limit 1) tmp 
SET 
    tsk.qty = (if(tmp.qty >= 1,
        (tmp.qty - 1),
        ifnull(tmp.qty, 0)))
WHERE
    tsk.id_stock = tmp.id_stock

提前致谢!

更新:

来自终端 - >

查询正常,1行受影响,2次警告(0.03秒) 匹配的行数:1已更改:1警告:0

注意(代码1592):自BINLOG_FORMAT = STATEMENT以来使用语句格式写入二进制日志的不安全语句。该语句不安全,因为它使用LIMIT子句。这是不安全的,因为无法预测包含的行集。 注意(代码1592):自BINLOG_FORMAT = STATEMENT以来使用语句格式写入二进制日志的不安全语句。从另一个表中选择后,使用自动增量列写入表的语句是不安全的,因为检索行的顺序决定了将写入哪些行(如果有)。此订单无法预测,可能在主设备和从设备上有所不同。

http://codetidy.com/8338/

1 个答案:

答案 0 :(得分:1)

没有想法。 但是让它尝试单独执行它们,就像你在上面的url

中提到的那样
        $mod_select = $this->db->mod_select("SELECT
       tsk.id_stock, tsk.qty
   FROM
       tbl_store ts
   inner join tbl_stock tsk ON ts.id_store = tsk.id_store
   where
       ts.id_physical_place = " . $all_userdata['id_physical_place'] . "
           and ts.store_status = 1
           and tsk.stock_status = 1
           and tsk.id_products = " . $value['itm'] . " limit 1");
        if ($mod_select[0]->qty >= $value['qty']) {
            $this->db->update('tbl_stock', array('qty' => $mod_select[0]->qty - $value['qty']), array('id_stock' => $mod_select[0]->id_stock));
        }

希望这有帮助。

相关问题