View Full Version : Golf records!
froth
05-03-2005, 02:46 AM
Say I have a table like this:
table DATA:
name | varchar | PRIMARY KEY
score| int
and I want to know what name is in second place. When I do a
SELECT name FROM data ORDER BY score ASC LIMIT 2,1;
mysql returns the name of the person in 2nd. But say I have the name and I want to know what place they're in. How could I do that?
Tangerine Dream
05-03-2005, 12:52 PM
SELECT name FROM data ORDER BY score ASC LIMIT 2,1;
mysql returns the name of the person in 2nd.
Hi, if you want the name of the person in 2nd place, i.e 2nd place from the top score 1st place and independent on how many rows in the table:
SELECT name FROM data ORDER BY score DESC LIMIT 1,1;
Check 13.1.7. SELECT Syntax (http://dev.mysql.com/doc/mysql/en/select.html):
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must be integer constants.
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
But say I have the name and I want to know what place they're in. How could I do that?
I can't imagine how to do it without second table and several queries like this:
CREATE TABLE memdata (
id SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL UNIQUE,
score INT NOT NULL
) TYPE = MEMORY;
INSERT memdata (name, score) SELECT name, score FROM data ORDER BY score DESC;
SELECT id FROM memdata WHERE name = '$playername';
TRUNCATE TABLE memdata;
Hope someone will post more elegant solution :)
vBulletin® v3.8.2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.