axiom82 Posted June 5, 2009 Share Posted June 5, 2009 I have a result table (which will be the subquery) that returns the count of all `users` who have a greater `count` than the value supplied. The objective is to return the `position` of the user with u.`id` given this subquery condition. Here I am using u.`count` as the conditional value. SELECT COUNT(*) FROM ( SELECT `id` FROM `users` WHERE `count` > u.`count` ) u.`count` is actually a value from the main query. u.count is not accessible in my subquery. How can I make it accessible. Below was my best idea... SELECT u.`id`, ( SELECT COUNT(*) FROM ( SELECT `id` FROM `users` WHERE `count` > u.`count` ) tbl ) AS `position` FROM `users` u WHERE u.`id`=1 If you look in the code above, I get a MySQL error stating that u.`count` is unknown...yet it is known to the parent query. Please help. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted June 5, 2009 Share Posted June 5, 2009 select `id`, count(*) as `count` from `users` having count(*) > (select count(*) from `users` where `id`=1) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.