如何使用来自另一个 table 的数据从 table 中获取某项的平均值
How to get an avarage of something from a table using data from another table
我有这个table
table A(price) and this table B(name)
我想使用 table B
的名称从 Table A 获取某物的平均价格
我这样试过
select AVG(a.price) from a,b where b.name='something'
但是这个 returns table A 中所有项目的平均值。
我也试过像这样使用 join
AVG(a.price) from a left join b on b.name='something'
但它 returns 和以前一样
您在加入中缺少一个密钥。 table A 中的商品价格必须与具有特定键的记录相对应,因此在 table b 中可以使用具有该名称的相同键。可能如下所示。
Select avg(price) from A
Join B on
A.key=B.key
Where B.name=‘something’
然后这又取决于您的 table 结构。
我有这个table
table A(price) and this table B(name)
我想使用 table B
的名称从 Table A 获取某物的平均价格我这样试过
select AVG(a.price) from a,b where b.name='something'
但是这个 returns table A 中所有项目的平均值。
我也试过像这样使用 join
AVG(a.price) from a left join b on b.name='something'
但它 returns 和以前一样
您在加入中缺少一个密钥。 table A 中的商品价格必须与具有特定键的记录相对应,因此在 table b 中可以使用具有该名称的相同键。可能如下所示。
Select avg(price) from A
Join B on
A.key=B.key
Where B.name=‘something’
然后这又取决于您的 table 结构。