• 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!

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

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    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

    #2
    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
    +50 Xeno Geek Points
    Come back Toolpusher, scotspine, Voodooflux. Pogle
    As for the rest of you - DILLIGAF

    Purveyor of fine quality smut since 2005

    CUK Olympic University Challenge Champions 2010/2012

    Comment


      #3
      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
      McCoy: "Medical men are trained in logic."
      Spock: "Trained? Judging from you, I would have guessed it was trial and error."

      Comment


        #4
        or assign the value returned by count to a variable and use that instead?

        Comment


          #5
          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
          +50 Xeno Geek Points
          Come back Toolpusher, scotspine, Voodooflux. Pogle
          As for the rest of you - DILLIGAF

          Purveyor of fine quality smut since 2005

          CUK Olympic University Challenge Champions 2010/2012

          Comment


            #6
            Personally I like temp tables. I know dbas hate them : but they make the code more readable.

            <dons tin hat>

            Comment


              #7
              i think it might be working....

              2 secs

              Comment


                #8
                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

                Comment


                  #9
                  Originally posted by minestrone View Post
                  Cheers guys

                  The subquery worked, just had to add in the tables with FROM in that though.
                  Doh!
                  +50 Xeno Geek Points
                  Come back Toolpusher, scotspine, Voodooflux. Pogle
                  As for the rest of you - DILLIGAF

                  Purveyor of fine quality smut since 2005

                  CUK Olympic University Challenge Champions 2010/2012

                  Comment

                  Working...
                  X