A Flash Developer Resource Site

Results 1 to 1 of 1

Thread: Can I do this in a single query?

  1. #1
    Stupid Little Dreamer
    Join Date
    Aug 2000
    Location
    Toronto
    Posts
    583

    Can I do this in a single query?

    At our website, registered users may choose to sign up for a season of premier membership. I'm trying to write a single query that will show some info about the user, how many premier seasons he or she has signed up for so far, and the date of the latest membership season. Something like, perhaps:
    Code:
    NAME.......................NUM SEASONS.................LATEST SEASON
    JOHN SMITH .................4 .........................SUMMER 2006
    I've written a simple query that will do this, but with one important flaw -- instead of showing the latest season, it shows whatever happened to be the first season it found before GROUPing the results. Here's what I mean.

    Suppose John Smith has signed up for Fall 2005, Winter 2006, Spring 2006 and Summer 2006, and that the seasons appear in the seasons table in that order. The following query will return John Smith, 4, Fall 2005:

    Code:
    SELECT USR_username, 
    	COUNT(UAS_singleID) AS 'numSeasonsPlayed',
    	SEA_longname
    FROM users
    	JOIN users_and_memberships
    		ON USR_userID = UAM_userID
    	JOIN seasons
    		ON UAM_seasonID = SEA_seasonID
    WHERE USR_userID = '100'
    GROUP BY USR_userID
    ORDER BY SEA_startdate DESC
    If I get rid of the COUNTing and merely wish to show John's most recent season, then it's no problem; the following query will return "John Smith, Summer 2006":

    Code:
    SELECT USR_username, SEA_longname
    FROM users
    	JOIN users_and_memberships
    		ON USR_userID = UAM_userID
    	JOIN seasons
    		ON UAM_seasonID = SEA_seasonID
    WHERE USR_userID = '100'
    ORDER BY SEA_startdate DESC
    LIMIT 1
    I'm assuming that what's going on here is that by the time the ORDER BY is reached, there's now only one record, thanks to the COUNT/GROUP BY combination. So -- other than simply writing a second query and handling things via PHP, is there a way around this? BTW I'm limited to MySQL 4.1.

    Many thanks!
    Last edited by canuckster; 06-26-2006 at 09:59 AM.
    If it weren't for the last minute, nothing would get done ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center