mySQL subquery problem

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default mySQL subquery problem

    HI, I am trying to join a bunch of queries together so that I can convert an
    html table into a cfgrid, and having problems. The query is:
    <cfquery name="getteams" datasource="#ds#">
    SELECT teams.teamname
    FROM teams
    WHERE teams.age_id = '#session.ageid#'
    (SELECT count(homescore, vistorscore) as wins
    FROM schedule
    WHERE homescore > visitorscore
    AND visitorscore < homescore)
    GROUP BY teams.team_id
    </cfquery>
    I know I probably have the format wrong but I have moved it everywhere and it
    does not seem to matter the error being reported is:
    You have an error in your SQL syntax; check the manual that corresponds to
    your MySQL server version for the right syntax to use near '(SELECT
    count(homescore, vistorscore) as wins from schedule where homescore ' at line 4


    The error occurred in C:\Program Files\Apache
    Group\Apache2\htdocs\LeagueTracking\results\result s.cfm: line 34

    32 : SELECT teams.teamname
    33 : FROM teams
    34 : WHERE teams.age_id = '#session.ageid#'
    35 : (SELECT count(homescore, vistorscore) as wins
    36 : from schedule

    Could someone assist me in getting going in the right direction? I have
    several other queries to add to this, and I am sure I can get the rest if I get
    going in the right direction. TIA.

    rmorgan Guest

  2. Similar Questions and Discussions

    1. converting a MySQL 4.1 subquery to something that'll work with MySQL 4.0
      Say I have the following SQL query: UPDATE phpbb_users SET user_nthpost = ( SELECT post_time FROM phpbb_posts WHERE phpbb_users.user_id =...
    2. MySQL subquery in select
      Hey NG, I got a tabel like this: +----+-------+--------+ | id | value | type | +----+-------+--------+ | 1 | 512 | art no |...
    3. Subquery
      I am trying to update a table with data in a "look up table." Specifically, I have a table that contains zip codes but is missing both city &...
    4. MySQL/PHP problem. getting field names from MySQL and using it asPHP variables
      Is this possible? I'd like to just get the field names from my database and use it as variables to save me from making errors as to where i should...
    5. subquery with more than 1 fields
      select * from ordermaster where (orderid,customerid) in select orderid,customerid from orderdetail the sql can't be run in ms sql and is there...
  3. #2

    Default Re: mySQL subquery problem

    well, assuming that you are running 4.1+ that supports subqueries...
    you cannot specify 2 columns in a count(), however you can use count(*).
    So, that being said, this should work:

    <cfquery name="getteams" datasource="#ds#">
    SELECT teams.teamname,
    (SELECT count(homescore) FROM schedule WHERE homescore > visitorscore
    AND visitorscore < homescore) as wins
    FROM teams
    WHERE teams.age_id = '#session.ageid#'
    GROUP BY teams.team_id
    </cfquery>

    HTH
    --
    Tim Carley
    [url]www.recfusion.com[/url]
    [email]info@NOSPAMINGrecfusion.com[/email]
    Mountain Lover Guest

  4. #3

    Default Re: mySQL subquery problem

    What version of MySQL is running on your server? Older versions don't support subqueries, at all.
    Kronin555 Guest

  5. #4

    Default Re: mySQL subquery problem

    Definetely bad syntax. Also, this:
    WHERE homescore > visitorscore
    AND visitorscore < homescore

    is redundant.

    What's the relationship between your teams and schedule table?

    Originally posted by: rmorgan
    HI, I am trying to join a bunch of queries together so that I can convert an
    html table into a cfgrid, and having problems. The query is:
    <cfquery name="getteams" datasource="#ds#">
    SELECT teams.teamname
    FROM teams
    WHERE teams.age_id = '#session.ageid#'
    (SELECT count(homescore, vistorscore) as wins
    FROM schedule
    WHERE homescore > visitorscore
    AND visitorscore < homescore)
    GROUP BY teams.team_id
    </cfquery>
    I know I probably have the format wrong but I have moved it everywhere and it
    does not seem to matter the error being reported is:
    You have an error in your SQL syntax; check the manual that corresponds to
    your MySQL server version for the right syntax to use near '(SELECT
    count(homescore, vistorscore) as wins from schedule where homescore ' at line 4


    The error occurred in C:\Program Files\Apache
    Group\Apache2\htdocs\LeagueTracking\results\result s.cfm: line 34

    32 : SELECT teams.teamname
    33 : FROM teams
    .34 : WHERE teams.age_id = '#session.ageid#'
    35 : (SELECT count(homescore, vistorscore) as wins
    36 : from schedule

    Could someone assist me in getting going in the right direction? I have
    several other queries to add to this, and I am sure I can get the rest if I get
    going in the right direction. TIA



    Dan Bracuk Guest

  6. #5

    Default Re: mySQL subquery problem

    Actually no, it won't work. First, you are selecting teamname and grouping by
    teamid, so that will crash. Second, there is nothing in your query that
    matches the team to the scores.

    Originally posted by: Newsgroup User
    well, assuming that you are running 4.1+ that supports subqueries...
    you cannot specify 2 columns in a count(), however you can use count(*).
    So, that being said, this should work:

    <cfquery name="getteams" datasource="#ds#">
    SELECT teams.teamname,
    (SELECT count(homescore) FROM schedule WHERE homescore > visitorscore
    AND visitorscore < homescore) as wins
    FROM teams
    WHERE teams.age_id = '#session.ageid#'
    GROUP BY teams.team_id
    </cfquery>

    HTH
    --
    Tim Carley
    [url]www.recfusion.com[/url]
    [email]info@NOSPAMINGrecfusion.com[/email]




    Dan Bracuk Guest

  7. #6

    Default Re: mySQL subquery problem

    >Actually no, it won't work. First, you are selecting teamname and grouping by
    teamid,
    >so that will crash.
    Dan,

    That is not strictly true. It depends on your database. This query is
    allowed in mySQL (at least in 4.1)

    SELECT teams.teamname
    FROM teams
    GROUP BY teams.team_id





    mxstu Guest

  8. #7

    Default Re: mySQL subquery problem

    oops, just pasted what was there, didn't really look at anything other
    than the subquery, I don't know his db design. I presume he does!
    --
    Tim Carley
    [url]www.recfusion.com[/url]
    [email]info@NOSPAMINGrecfusion.com[/email]
    Mountain Lover Guest

  9. #8

    Default Re: mySQL subquery problem

    Thank you guys, but I got it figured out. I turned 265 lines of code for html
    presentataion to this:
    select team as teamname, sum(wins) as wins, sum(loss) as loss, sum(tie) as
    tie, sum(forfeit) as forfeit, sum(GP) as GP, sum(GF) as GF, sum(GA) as GA,
    (sum(GF)-sum(GA)) as GD, (sum(GF) / sum(GP)) as avggf, (sum(GA) / sum(GP)) as
    avgga, (sum(wins)*3 + sum(tie)*2 + sum(loss)*1) as Points
    from (select home as team,
    sum(if(homescore>visitorscore,1,0)) as wins,
    sum(if(visitorscore>homescore,1,0)) as loss,
    sum(if(homescore=visitorscore,1,0)) as tie,
    sum(if(homeforfeit=1,1,0))as forfeit,
    count(home) as GP,
    sum(homescore)as GF,
    sum(visitorscore) as GA
    from schedule group by team
    union
    select visitor as team,
    sum(if(homescore < visitorscore,1,0)) as wins,
    sum(if(visitorscore < homescore,1,0)) as loss,
    sum(if(homescore=visitorscore,1,0)) as tie,
    sum(if(visitorforfeit=1,1,0))as forfeit,
    count(visitor) as GP,
    sum(visitorscore) as GF,
    sum(homescore)as GA
    from schedule group by team) as table1 group by team
    its adding everything perfectly, I just needed a nudge. TXS GUYS


    rmorgan Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139