您可以使用where子句和IN运算符来查找表2中id为且颜色为蓝色的特定产品的成本总和。
简单地说,蓝色的产品ID应该在查询1中找到:
SELECT table1.id from table1 where table1.colour = "blue";
成本加总:
select sum(cost) from table2;
加入关键字:
select sum(cost) from table2 where table2.id in (SELECT table1.id from table1 where table1.colour = "blue")
in运算符的工作方式类似于where子句查询中的循环和匹配id。