MySQL Error 1054求助:关联查询SQL修正方案咨询
Fix for MySQL Error 1054 in Your Query
The error happens because you’ve aliased your filtered price_change subquery as T, but you’re still trying to reference the original price_change table in the JOIN condition. Once you assign an alias to a subquery, you must use that alias to refer to its columns instead of the original table name.
Here’s the corrected query:
SELECT T.listing_id, (AVG((T.new_price - T.old_price)/T.old_price)) / bua.built_area AS price_change_per_sqft FROM ( SELECT listing_id, new_price, old_price FROM price_change WHERE YEAR(change_date) = 2016 AND new_price > old_price ) AS T JOIN built_used_area AS bua ON T.listing_id = bua.listing_id GROUP BY T.listing_id, bua.built_area;
Key Improvements & Fixes:
- Replaced
price_change.listing_idwithT.listing_idin the JOIN clause (sinceTis the alias for your filtered subset ofprice_change) - Added a clear alias (
bua) for thebuilt_used_areatable to make the query more readable - Included
GROUP BYto calculate the average price change per individual listing (divided by its corresponding built area). If you instead want the overall average across all qualifying records divided by each listing’s area, remove theGROUP BYclause andT.listing_idfrom the SELECT statement.
Why This Works:
When you create a subquery with an alias like AS T, MySQL treats that subquery as a temporary table named T. Any references to columns from that subquery must use the alias T—the original table name price_change is no longer valid in that context.
内容的提问来源于stack exchange,提问作者Eldiias Dzhamankulov
相关产品推荐
相关产品推荐

