Jump to content

Please help: Advanced MySQL Subquery


axiom82

Recommended Posts

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. :)

Link to comment
https://forums.phpfreaks.com/topic/161022-please-help-advanced-mysql-subquery/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.