php - Unknown column 'stockout_details.purchi' in 'on clause'

i'm using this query but i found this error Unknown column 'stockout_details.purchi' in 'on clause'
SELECT sale_nag,stockin_details.purchi,
COALESCE(stockin_details.sale_nag,0) + stockin_details.sale_nag result
FROM stockin_details LEFT JOIN
(SELECT SUM(sale_nag) AS snag
FROM stockout_details
WHERE purchi='a12'
GROUP BY marka,chalan,room,rack
) stockout_details
ON stockin_details.purchi=stockout_details.purchi
WHERE stockin_details.purchi='a12'
GROUP BY marka,chalan,room,rack
Answer
Solution:
You need to include the column in the subquery:
SELECT o.sale_nag, i.purchi,
COALESCE(i.sale_nag,0) + i.sale_nag result
FROM stockin_details i LEFT JOIN
(SELECT purchi, SUM(sale_nag) AS snag
FROM stockout_details
WHERE purchi = 'a12'
GROUP BY marka, chalan, room, rack, purchi
) o
ON i.purchi = o.purchi
WHERE i.purchi = 'a12'
GROUP BY i.marka, i.chalan, i.room, i.rack;
Note that I replaced the table aliases with more manageable aliases and qualified all column names.
That said, I'd be surprised if the query actually did anything useful -- theresult
is only using values from one table.
Also, I would expect thejoin
conditions to use all the columns in the aggregation. If you want further help with the query, ask a new question. Provide sample data, desired results, an explanation of what the code should do, and an appropriate database tag.
Answer
Solution:
You need to include column stockout_details.purchi in your select then only you can use it in join clause as you are creating subquery and this is what Gordon was trying to highlight.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the metadata storage is not up to date, please run the sync-metadata-storage command to fix this issue.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.