SQL通过id从另一个表中检索值并显示总数

时间:2018-02-17 05:43:07

标签: mysql sql

通过id从另一个表中检索值并显示总数(通过获取id并根据id从table2获取值来迭代table1)

场景 - 获取发票ID 987的总和

pid金额在表2中,我想得到总数987

表1 view
enter image description here

表2 view
enter image description here

2 个答案:

答案 0 :(得分:0)

Try this -

$sql = "SELECT SUM(t1.Amount) FROM t1 LEFT JOIN e1 ON t1.PID = e1.PID WHERE e1.InvoiceID='987' GROUP BY e1.InvoiceID";

答案 1 :(得分:0)

SELECT a.id, sum(b.amount)
FROM e1 a
INNER JOIN t1 b ON a.pid = b.pid
group by a.id;