更新内部加入两个表

时间:2016-04-26 23:47:19

标签: php mysql sql

**表输入**

(id_inpits, name, quantity_inputs)

表格输出 `(id_outputs,name_outputs,quantity_outputs,quantity_disponible)

当我想更新quantity_output并计算quantity_disponbile

知道:quantity_disponbile = quantity_inputs - quantity_output

我尝试过:

if(!empty($_POST['do'])) {

    $m_id = $_POST['id_output'];

    $quantity_outputs = $_POST["quantity_outputs"];

    $sql = $db->query("UPDATE outputs AS o INNER JOIN inputs As i ON i.id_input = o.inputs_id  SET o.quantity_dispo = 'select quantity_inputs from inputs - FROM (select quantity_outputs from outputs )', o.quantity_outputs = '$quantity_outputs'  WHERE o.id_output =' $m_id'"); 

    if(!$sql) {
        die(mysql_error());
    }
}

1 个答案:

答案 0 :(得分:0)

一个建议:quantity_disponbile应该是两个表中的计算字段。因此将逻辑拆分为两个:一个选择查询和一个更新查询。更新查询只需要更新quantity_output,选择查询可以是:

select 
  i.quantity_inputs -  o.quantity_output = quantity_disponible
from output o 
    inner join input i on i.id_inpits = o.inputs_id

我无论如何都不能纠正错字