X__
X__

Reputation: 89

SQL: Reduce query results to single item

I'm querying an SQL database for an online gaming site. I want to find the game with the oldest user login date (found using min(charstat_last_activated) ) and return the associated unique game identifier (charstat_game). Unfortunately the min function is causing me some problems - whenever I try to create a subquery to return only the charstat_game entry I get an error message.

Below is the code that returns both the oldest game on record and the game id. If somebody can tell me how to adapt this to return only the charstat_game portion from the oldest game then I'd be very grateful.

SELECT charstat_game, min( charstat_last_activated ) 
FROM `character_main` 
WHERE charstat_player =11
AND charstat_active =1
AND charstat_game >0

Upvotes: 0

Views: 385

Answers (1)

iruvar
iruvar

Reputation: 23374

Use a subquery along the following lines

select charstat_game from `character_main` where charstat_last_activated = (
SELECT  min( charstat_last_activated ) 
FROM `character_main` 
WHERE charstat_player =11
AND charstat_active =1
AND charstat_game >0
)
and charstat_player =11
AND charstat_active =1
AND charstat_game >0

Upvotes: 2

Related Questions