Sila Khatun Sila Khatun - 1 year ago 62
SQL Question

SQL Sub query Issue

Unable to catch Sub Query output on where

There is my SQL code

SELECT Txn.TrnID, Txn.Date, Txn.Amount,
(SELECT `MetaValue` FROM `meta` WHERE `Parent` = 'DB_Transaction' AND `MainID` = Txn.TrnID AND `MetaKey` = 'SalesID' AND `MetaValue` = '803') AS SubqueryResult
FROM transaction as Txn
WHERE SubqueryResult = '803'

I'm getting this error

Unknown column 'SubqueryResult' in 'where clause'

Answer Source

May you can try Dr. Stitch. which is

SELECT Txn.TrnID, Txn.Date, Txn.Amount, m.MetaValue FROM transaction as Txn inner join meta as m    on m.Parent = 'DB_Transaction' and m.MainID = Txn.TrnID AND m.MetaKey = 'SalesID' AND m.MetaValue = '803' LIMIT 10