2014-03-07 107 views
0

查詢下方在mysql終端,工作臺和php中執行不同的方式。 mysql終端和工作臺給出相同的結果,但php給出不同的結果(這似乎像查詢執行2次)。 我想從數量中減少1項。但在php中(codeigniter,pdo連接)它從數量中減去2個項目(總是我提到的兩倍)。 注意:我很確定我沒有執行這個查詢兩次(在一個循環中)。Mysql更新查詢運行兩次

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

顯示相關的php代碼。我希望你只是簡單地運行兩次php代碼。 – Kami

+0

應該是php的問題 – Aris

+0

哪裏的php代碼?如果沒有php代碼,爲什麼php被標記? – FreshPro

回答

1

沒有想法。 但給它嘗試執行它們分開,你上面提到的網址在

 $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)); 
     } 

希望這有助於。