Saving time by using MySQL query command and the WHERE function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • intrepid21
    New Member
    • Jul 2014
    • 7

    Saving time by using MySQL query command and the WHERE function

    I have a database with some products in it. I can update one of the
    products with a SQL query by the command

    Code:
    UPDATE `mytablename` SET `price` = '7.99' WHERE `id` =26 LIMIT 1 ;

    In order for me to update about 100 products to a price '3.99' I have to
    go in excel and do some concatenations( linking) and then copy and paste a hundred
    individual command lines of

    Code:
    UPDATE `mytablename` SET `price` = '3.99' WHERE `id` =26 LIMIT 1 ;
    UPDATE `mytablename` SET `price` = '3.99' WHERE `id` =27 LIMIT 1 ;
    UPDATE `mytablename` SET `price` = '3.99' WHERE `id` =28 LIMIT 1 ;
    UPDATE `mytablename` SET `price` = '3.99' WHERE `id` =29 LIMIT 1 ;
    UPDATE `mytablename` SET `price` = '3.99' WHERE `id` =30 LIMIT 1 ;
    UPDATE `mytablename` SET `price` = '3.99' WHERE `id` =100 LIMIT 1 ;
    Is there a command where I can just type one line such as


    Code:
    UPDATE `mytablename` SET `price` = '3.99' WHERE `id` =26-100 LIMIT 1 ;
    Do I need to use the WHERE Command?

    I am new to MySQL but really want to learn it becaue manipulating it in
    excel is taking a long time. Anything would help.

    Thanks
    Last edited by Rabbit; Jul 16 '14, 08:27 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Code:
    `id` BETWEEN 26 AND 100

    Comment

    • intrepid21
      New Member
      • Jul 2014
      • 7

      #3
      Thanks Rabbit! That worked.

      Comment

      • intrepid21
        New Member
        • Jul 2014
        • 7

        #4
        Lets say I have 100 products with 'id' 1-100 that are mostly 7.99 (price) and every once in a while have a product for 14.99 in that 'id' range

        I would like to change the price of the ones listed at 7.99 to 3.99 and the ones listed at 14.99 to 6.99 within that 'id' range of 1-100

        I know I can use the UPDATE and BETWEEN function such as

        Code:
        UPDATE `mytablename` SET `price` = '3.99' WHERE `id` BETWEEN 1 AND 9
        but if the 10th 'id' where 14.99 I would have to run another single command

        Code:
        UPDATE `mytablename` SET `price` = '6.99' WHERE `id` =10 LIMIT 1 ;
        Is there a way to combine these into one single command so that it will look for all the 7.99 prices within that 'id' range and UPDATE them to 3.99 and look for all the 14.99 prices within that 'id' range and update them to 6.99

        This would save me so much time!
        Last edited by Rabbit; Jul 16 '14, 08:28 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          Code:
          CASE `price`
             WHEN 7.99 THEN 3.99
             WHEN 14.99 THEN 6.99
             ELSE `price`
          END CASE

          Comment

          • intrepid21
            New Member
            • Jul 2014
            • 7

            #6
            So would it look like this

            Code:
            UPDATE `mytablename` SET `price` = '3.99' WHERE `id` BETWEEN 1 AND 100 
            
                CASE `price`
                   WHEN 7.99 THEN 3.99
                   WHEN 14.99 THEN 6.99
                   ELSE `price`
                END CASE
            I'm just confused where to set the range for the 'id' because I have thousands of other products within the table that I don't want to update
            Last edited by Rabbit; Jul 16 '14, 08:28 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              Code:
              SET `price` = CASE ...
              You already set the range on id with the BETWEEN.

              Comment

              • intrepid21
                New Member
                • Jul 2014
                • 7

                #8
                I am getting syntax errors with the following

                Code:
                UPDATE `mytablename` SET `price` = 
                
                CASE `price`
                WHEN 7.99 THEN 3.99
                WHEN 14.99 THEN 6.99
                ELSE `price`
                END CASE
                #1064 - 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 'CASE' at line 7



                Also when I try it like this

                Code:
                UPDATE `mytablename` SET `price` = 
                
                CASE `price` WHERE `id` BETWEEN 1 AND 100
                WHEN 7.99 THEN 3.99
                WHEN 14.99 THEN 6.99
                ELSE `price`
                END CASE
                #1064 - 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 'WHERE `id` BETWEEN 1 AND 100
                WHEN 7.99 THEN 3.99
                WHEN 14.99 THEN 6.99
                ELSE `p' at line 3

                Any hint or solution would help...
                Last edited by Rabbit; Jul 16 '14, 08:29 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  Please use tags when posting code or formatted data.

                  In your first attempt, your error code says line 7 when it looks like it means line 3. Which means that the code you posted is not the code you tried to run.

                  In your second attempt, for some reason you put the WHERE condition inside the CASE when I didn't do that in my code.

                  It looks like you are having a lot of trouble with basic SQL. Before you take on a project like this, you should probably learn the basics first.

                  Here is a tutorial you can use to learn the basics: http://www.w3schools.com/sql/

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    It looks like in MySQL, the end of the CASE statement is just END and not END CASE.

                    Comment

                    • intrepid21
                      New Member
                      • Jul 2014
                      • 7

                      #11
                      Thanks Rabbit. I have read the tutorial and doesn't have much on using the CASE with the BETWEEN functions

                      The code below works great

                      Code:
                          UPDATE `mytablename` SET `price` = 
                           
                          CASE `price` 
                          WHEN 7.99 THEN 3.99
                          WHEN 14.99 THEN 6.99
                          ELSE `price`
                          END
                      However this will update every ProductID with a price of 7.99 to 3.99 and every ProductId that is 14.99 to 6.99 which is great but I'm only trying to update a range of ProductIds from over 10,000

                      The BETWEEN function I used earlier would also only update a range of ProductID's with one value

                      Code:
                       UPDATE "mytablename` SET `price` = '3.99' WHERE `id` BETWEEN 3000 AND 5000
                      Do you have any other references on using the BETWEEN with CASE

                      Comment

                      • intrepid21
                        New Member
                        • Jul 2014
                        • 7

                        #12
                        Rabbit Thanks for your help! I'm an novice to this but it worked out.

                        Code:
                        UPDATE `mytable` SET `price` =    
                         (
                                  CASE `price`
                               WHEN 3.99 THEN 1.99
                               WHEN 9.99 THEN 6.99
                               ELSE `price`
                            END 
                          )
                        WHERE `id` BETWEEN 2000 AND 4000
                        Thanks again :)

                        Comment

                        • Rabbit
                          Recognized Expert MVP
                          • Jan 2007
                          • 12517

                          #13
                          Glad you were able to work it out. Good luck on the rest of your project.

                          Comment

                          Working...