• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:

  • You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
  • You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
  • If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.

Previously on "SQL help (updating using a count() in mySQL )"

Collapse

  • Zippy
    replied
    Originally posted by minestrone View Post
    Cheers guys

    The subquery worked, just had to add in the tables with FROM in that though.
    Doh!

    Leave a comment:


  • minestrone
    replied
    Cheers guys

    The subquery worked, just had to add in the tables with FROM in that though.

    First time I have user mySQL stored procedures, usually I would have a update, set, from where but mySQL wants the from tables in the update bit

    Had to go into the system and add some data under a diff user to test, you know you have been on this site too long when your test users are sockpuppet1, sockpuppet2 etc.

    Table A is actually a temp table as well which gets built up with a few selects.

    Cheers

    Leave a comment:


  • minestrone
    replied
    i think it might be working....

    2 secs

    Leave a comment:


  • BrilloPad
    replied
    Personally I like temp tables. I know dbas hate them : but they make the code more readable.

    <dons tin hat>

    Leave a comment:


  • Zippy
    replied
    Originally posted by wurzel View Post
    or assign the value returned by count to a variable and use that instead?
    I think the value for the count will vary from row to row

    Leave a comment:


  • wurzel
    replied
    or assign the value returned by count to a variable and use that instead?

    Leave a comment:


  • lilelvis2000
    replied
    I think this could work - I'm not a mySQL expert.

    update
    A a , B b
    set
    a.count = (SELECT count(c.id)
    where
    b.id = c.b_id )
    where
    a.id = b.a_id

    Leave a comment:


  • Zippy
    replied
    Originally posted by minestrone View Post
    First off I know I am rotten at SQL and have managed to busk it for most of my career.

    I am trying to write a stored proc and want to update a column with the count of lines for that condition

    update
    A a , B b , C c
    set
    a.count = count(c.id)
    where
    a.id = b.a_id AND
    b.id = c.b_id ;

    I get a "Invalid use of group function" error

    If I set the line to

    a.count = 1

    I inserts to the table OK. thanks for any help
    Try getting the count in a subquery?
    update
    A a , B b , C c
    set
    a.count = (SELECT count(c.id)
    where
    a.id = b.a_id AND
    b.id = c.b_id )
    where
    a.id = b.a_id AND
    b.id = c.b_id

    I'm sure somebody will be along with a better idea soon

    Leave a comment:


  • minestrone
    started a topic SQL help (updating using a count() in mySQL )

    SQL help (updating using a count() in mySQL )

    First off I know I am rotten at SQL and have managed to busk it for most of my career.

    I am trying to write a stored proc and want to update a column with the count of lines for that condition

    update
    A a , B b , C c
    set
    a.count = count(c.id)
    where
    a.id = b.a_id AND
    b.id = c.b_id ;

    I get a "Invalid use of group function" error

    If I set the line to

    a.count = 1

    I inserts to the table OK. thanks for any help
Working...
X