添加一列的值并插入不同的表mysql

时间:2014-02-22 15:47:13

标签: php mysql sql

我有2个表顾客和牛奶,如下所示

客户

customers (cid, name, price, total);

milk (mid, customer_id, milk_letters, mprice)

我正在尝试

$cid = $_POST['cid'];

我想将客户表更新为牛奶的总和(mprice),其中customer_id = cid

1 个答案:

答案 0 :(得分:0)

sql代码:

UPDATE customers c
SET total=(SELECT SUM(mprice) FROM milk WHERE customer_id=c.cid)
WHERE c.cid=$cid

也使用intval

$cid = intval($_POST['cid']);