I’ve never figured out several syntax things about SQL subqueries. Mainly I’m interested in where in the parent query is it valid to place a subquery.
here’s an example which throws an error:
SELECT
sum(votes.vote) AS sum,
votes.vote IS NOT NULL AS did_i_vote,
purchase_id, subject, full_name
FROM (
SELECT vote FROM votes
where votes.acct_id=3 AND
votes.column_name='purchase_id'
) votes
RIGHT JOIN items_purchased
ON votes.parent_id=items_purchased.purchase_id
JOIN accounts
ON items_purchased.purchaser_account_id=accounts.acct_id
JOIN items
ON items_purchased.item_id=items.folder_id
WHERE purchase_id='2'
GROUP BY items_purchased.purchase_id
How do I make this query work?
Your
subquerymust select everycolumnthat you wish to reference afterwards.Is what I would assume (note that I select
voteandparent_idin the subquery)