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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!