dra11767 2014-03-07 13:13
浏览 54
已采纳

Mysql Update Query运行两次

Below Query Execute different ways in mysql terminal, workbench and php. mysql terminal and workbench gives same result, but php gives different result(it seems like query executes 2 times). I want to decrease 1 item from quantity. but in php(codeigniter ,pdo connection) it substracts 2 items from quantity (always twice as i mentioned). Note: I'm pretty sure that I'm not executes this query twice(in a loop).

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

Thanks in advance!

update:

from terminal ->

Query OK, 1 row affected, 2 warnings (0.03 sec) Rows matched: 1 Changed: 1 Warnings: 0

Note (Code 1592): Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. The statement is unsafe because it uses a LIMIT clause. This is unsafe because the set of rows included cannot be predicted. Note (Code 1592): Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. Statements writing to a table with an auto-increment column after selecting from another table are unsafe because the order in which rows are retrieved determines what (if any) rows will be written. This order cannot be predicted and may differ on master and the slave.

http://codetidy.com/8338/

  • 写回答

1条回答 默认 最新

  • douying6206 2014-03-08 04:47
    关注

    NO Idea. But give it try to execute them separately as you mentioned in above 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));
            }
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角