a sql question anyone?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zester
    Confirmed User
    • Jul 2003
    • 5344

    #1

    a sql question anyone?

    I wonder how many of you know SQL.

    $sql="
    UPDATE records set appear_order='3' WHERE id='19' ;
    UPDATE records set appear_order='5' WHERE id='16' ;
    UPDATE records set appear_order='4' WHERE id='17' ;
    UPDATE records set appear_order='6' WHERE id='20' ;
    UPDATE records set appear_order='2' WHERE id='18' ;
    ";

    is there a better shorter way to do this query? can all these updated even be done at once?
    * Mainstream ? $65 per sale
    * new male contraception
  • Fuckin Bill
    Confirmed User
    • Feb 2003
    • 1020

    #2
    I think the way you've got it there is probably the best you're going to get. You may be able to optimize code around that in the program, but I don't think you can really get any tighter than that with the sql query itself, since all the values and records are different.

    Comment

    • MGPspots
      Confirmed User
      • Jan 2005
      • 422

      #3
      Good question... but I don't think there's a quick way since they are different rows in the table. Haven't seen any sql looping commands...
      SIG TOO BIG! Maximum 120x60 button and no more than 3 text lines of DEFAULT SIZE and COLOR. Unless your sig is for a GFY top banner sponsor, you may use a 624x80 instead of a 120x60. Let me repeat... A 120 x 60 button and no more that 3 lines of DEFAULT SIZE AND COLOR text.

      Comment

      • swedguy
        Confirmed User
        • Jan 2002
        • 7981

        #4
        If you have different where clauses, that's the way you have to do it.

        Comment

        • Zester
          Confirmed User
          • Jul 2003
          • 5344

          #5
          i thought i'd ask before I update 8747 rows
          * Mainstream ? $65 per sale
          * new male contraception

          Comment

          • Zester
            Confirmed User
            • Jul 2003
            • 5344

            #6
            8748 that is (just made another sale while posting)
            * Mainstream ? $65 per sale
            * new male contraception

            Comment

            • swedguy
              Confirmed User
              • Jan 2002
              • 7981

              #7
              hmm, there might be a way. Are you changing appear_order from one var to another var? Like all 1's to 5's or something similair?

              Comment

              • Warden
                Confirmed User
                • Nov 2002
                • 2906

                #8
                on an update, if you are updating every single record just leave out the WHERE statement
                AIM: ZeeRiddler
                ICQ: 128160005
                Warden's MS

                Comment

                • swedguy
                  Confirmed User
                  • Jan 2002
                  • 7981

                  #9
                  Originally posted by Warden
                  on an update, if you are updating every single record just leave out the WHERE statement
                  appear_order is different in his example, so that will not work. But what you're saying might work if for example all 1's are changed to 5's, 2 to 9 and so on.

                  Comment

                  • beergood
                    Confirmed User
                    • Jun 2003
                    • 2918

                    #10
                    I think what you're trying to do is called "Batch Updates".
                    icq: 320340263

                    Comment

                    • Warden
                      Confirmed User
                      • Nov 2002
                      • 2906

                      #11
                      Originally posted by swedguy
                      appear_order is different in his example, so that will not work. But what you're saying might work if for example all 1's are changed to 5's, 2 to 9 and so on.

                      Disregard. I just quick glanced at it and realized just now that there are unique values to that column.
                      AIM: ZeeRiddler
                      ICQ: 128160005
                      Warden's MS

                      Comment

                      • Zester
                        Confirmed User
                        • Jul 2003
                        • 5344

                        #12
                        Originally posted by Warden
                        Disregard. I just quick glanced at it and realized just now that there are unique values to that column.
                        yes , different values BUT the appear_id's are increasing values by 1 each time
                        * Mainstream ? $65 per sale
                        * new male contraception

                        Comment

                        • RefaStud
                          Confirmed User
                          • May 2003
                          • 183

                          #13
                          if you are just updating the appear_id's and nothing else you can do something like this.

                          UPDATE records set appear_order=appear_order + 1;


                          that assumes you want to auto_increment the number by 1 in EVERY ROW in the DB;
                          SIG TOO BIG! Maximum 120x60 button and no more than 3 text lines of DEFAULT SIZE and COLOR. Unless your sig is for a GFY top banner sponsor, then you may use a 624x80 instead of a 120x60.

                          Comment

                          • Zester
                            Confirmed User
                            • Jul 2003
                            • 5344

                            #14
                            Originally posted by RefaStud
                            if you are just updating the appear_id's and nothing else you can do something like this.

                            UPDATE records set appear_order=appear_order + 1;


                            that assumes you want to auto_increment the number by 1 in EVERY ROW in the DB;

                            nope, still need to specify ID
                            * Mainstream ? $65 per sale
                            * new male contraception

                            Comment

                            • Zester
                              Confirmed User
                              • Jul 2003
                              • 5344

                              #15
                              wait, the SQL query does'nt work. i get:

                              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 'UPDATE records set appear_order='2' WHERE id='19'; UPDATE recor

                              so it can't do more then 1 action in a query??
                              * Mainstream ? $65 per sale
                              * new male contraception

                              Comment

                              • RefaStud
                                Confirmed User
                                • May 2003
                                • 183

                                #16
                                nope. you will have to do a loop through each of the id's you wan't to update.


                                my $sql = "update appear_count='?' where ID='?'";
                                my $sth = $dbh->prepare($sql);
                                foreach my $ID (@ids){
                                $sth->execute(APPEAR_COUNT, $ID);
                                }
                                Last edited by RefaStud; 02-07-2005, 01:42 PM. Reason: didn't finish.. UGH
                                SIG TOO BIG! Maximum 120x60 button and no more than 3 text lines of DEFAULT SIZE and COLOR. Unless your sig is for a GFY top banner sponsor, then you may use a 624x80 instead of a 120x60.

                                Comment

                                Working...