1

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/

5
  • 1
    Show you relevant php code. I expect you are simply running the php code twice.
    – Kami
    Mar 7, 2014 at 13:15
  • should be php problem
    – Aris
    Mar 7, 2014 at 13:57
  • Where's the php code? And If there's none php code, why php is tagged?
    – FreshPro
    Mar 7, 2014 at 15:51
  • codetidy.com/8338 Mar 7, 2014 at 16:01
  • I didnt understood the use of $dataArray['product_Name_id_' . $index] ..What it function there actually ? Mar 8, 2014 at 5:42

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.