我有以下数据库架构:
Customer(ssn, name, gender, city)
Vehicle(vin, make, model, year)
BuyVehicle(bvssn, bvvin, price, year)
BuyVehicle.bvvin
外键从哪里来,Vehicle.vin
是BuyVehicle.bvssn
外键从哪里来Customer.ssn
。我正在尝试选择以高于该特定车辆平均价格的价格购买车辆的人的姓名。
到目前为止,我已经确定了如何计算车辆的平均值:
SELECT AVG(price) as avg_price, v.maker, v.model, v.year FROM BuyVehicle bv, Vehicle v, Customer c WHERE v.vin = bv.BVVIN AND c.ssn = bv.bvssn GROUP BY maker, model, year;
我试图在子句中输入一个price > avg_price
条件:WHERE
SELECT AVG(price) as avg_price, v.maker, v.model, v.year FROM BuyVehicle bv, Vehicle v, Customer c WHERE v.vin = bv.BVVIN AND c.ssn = bv.bvssn AND bv.price > avg_price GROUP BY maker, model, year;
但 MySQL 告诉我该avg_price
列不存在。我对这个问题采取了错误的方法吗?