Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    74
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Add 15% to a price column in db

    Hello,
    I have a db with products and I need to add 15% (column 'price') to some products from db, depending on category (column 'category').
    How can I do that instead of adding manual to each row?
    I use PhpMyAdmin if does any matter.
    Thank you in advance.
    Regards

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Unless this is a *PERMANENT* price change, DO NOT DO IT!

    If this is, for example, for the purpose of having a temporary surcharge on certain category(ies), do it in the SQL query, instead:
    Code:
    SELECT productid, 
           productname, 
           price,
           IF(category IN ('bedding','furniture'), '15%', 'None') AS surcharage,
           price * IF(category IN ('bedding','furniture'), 1.15, 1.0) AS priceWithSurcharge
    FROM products
    However, if it is permanent, then:
    Code:
    UPDATE products SET price = price * 1.15 WHERE category IN ('bedding','furniture')
    Last edited by Old Pedant; 08-28-2013 at 11:35 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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