Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Ranking position in a SQL index

by sgifford (Prior)
on Oct 05, 2004 at 04:41 UTC ( [id://396470]=note: print w/replies, xml ) Need Help??


in reply to Ranking position in a SQL index

It depends on how your indexes are formed and on your database's optimizer, but something like this might work:
SELECT COUNT(*) FROM table WHERE numeric_column < (SELECT numeric_column FROM table WHERE your_criteria)

In my version of mysql, subqueries aren't supported, so I actually have to save the subselect results into a variable, then do the second select, but EXPLAIN says this similar query is reasonably well-optimized:

mysql> explain select count(*) FROM faar_homes WHERE price < 80000 \G *************************** 1. row *************************** table: faar_homes type: range possible_keys: price key: price key_len: 5 ref: NULL rows: 25 Extra: where used; Using index

Replies are listed 'Best First'.
Re^2: Ranking position in a SQL index
by pg (Canon) on Oct 05, 2004 at 04:53 UTC
    "SELECT COUNT(*) FROM table WHERE numeric_column < (SELECT numeric_column FROM table WHERE your_criteria)"

    I believe you knew, but just to make it complete. The criteria has to make sure that only one row is returned from the subquery. Otherwise you would get error like 'too many rows returned'.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://396470]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2024-04-24 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found