sql trouble

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bob@coolgroups.com

    #1

    sql trouble

    Could someone help me get the following SQL statement working?

    SELECT
    standardgame.ga mename,
    standardgame.ro wteamname,
    standardgame.co lteamname,
    standardgame.do llarvalue,
    standardgame.ga meid,
    standardgame.cu toffdatetime,
    standardgame.ga metype,
    standardgame.ga meowner,
    (100-COUNT(purchased squares.gameid) ) AS squaresremainin g
    FROM standardgame
    LEFT OUTER JOIN
    purchasedsquare s ON standardgame.ga meid = purchasedsquare s.gameid
    where gametype='$game type' and dollarvalue = '$dollarvalue' and
    gameowner = '
    GROUP BY standardgame.ga meid
    order by
    CASE squaresremainin g WHEN 0 THEN 1 ELSE 0 END ASC,
    squaresremainin g ASC


    The problem is... MySQL doesn't seem to want to let me use
    squaresremainin g in that case statement since it's not an official
    column name. Any idea how I can reference squaresremainin g in the case
    statement?

  • Bill Karwin

    #2
    Re: sql trouble

    bob@coolgroups. com wrote:[color=blue]
    > The problem is... MySQL doesn't seem to want to let me use
    > squaresremainin g in that case statement since it's not an official
    > column name. Any idea how I can reference squaresremainin g in the case
    > statement?[/color]

    Hmm. Apparently you can't use column aliases in expressions in the
    ORDER BY clause, even though you can use the column aliases in ORDER BY
    without being in an expression.

    You could add another computed column `squaresremaini ng_sortorder` that
    contains the 1 or 0 you need to sort by? You can't reference the column
    alias in an expression in the select-list either, but you can use a
    duplicate expression. For example:

    SELECT ...
    (100-COUNT(purchased squares.gameid) ) AS squaresremainin g,
    CASE (100-COUNT(purchased squares.gameid) ) WHEN 0 THEN 1 ELSE 0 END
    AS squaresremainin g_sortorder
    FROM standardgame
    ...
    ORDER BY squaresremainin g_sortorder ASC, squaresremainin g ASC

    Regards,
    Bill K.

    Comment

    Working...