The query I really need to execute is follows:
SELECT u.points
(SELECT COUNT(1) FROM (SELECT 1 FROM checkin c INNER JOIN wineries w ON w.id = c.winery_id WHERE c.user_id = u.id GROUP BY region_id) b) as states_visited
FROM users u
GROUP BY u.id
ORDER BY points DESC
However, this causes the following error:
Unknown column 'u.id' in 'where clause'
I've tried with user-defined variables, no errors, but it's not actually referencing the user-defined variable value for some reason:
SELECT @uid := u.id, u.points
(SELECT COUNT(1) FROM (SELECT 1 FROM checkin c INNER JOIN wineries w ON w.id = c.winery_id WHERE c.user_id = @uid GROUP BY region_id) b) as states_visited
FROM users u
GROUP BY u.id
ORDER BY points DESC
Any thoughts how I can make this work? Without the obvious resorting to doing two separate queries?