1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

simple subtraction not working in mysql query

Discussion in 'MySQL' started by xbat, Oct 9, 2013.

  1. #1
    UPDATE table1
    INNER JOIN  table2 ON table1.columna=table2.columnd
    SET table1.columnb=table2.columne - table2.columnf / 2 + table2.columnf
    Code (markup):


    For some reason it will not subtract correctly in the query not sure what I am missing.

    columne columnf
    45 90
    60 90

    example taken from the first line above..
    45-90/2+ = 67.50<< in the calculator I am not getting the same amount in the query. It seems like the numbers aren't subtracting when it goes negative so in other words negative numbers are looking positive.. I think the database is reading -5.00 as 5.00 instead.. Any ideas or suggestions?
     
    xbat, Oct 9, 2013 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    did you have "bodmas" when you were a kid at school...

    if you need more control over the order that the equation is handled put some brackets in.
     
    sarahk, Oct 11, 2013 IP
  3. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #3

    Could you please give me a example where I need to put the brackets? As for school no.
     
    xbat, Oct 14, 2013 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    at a guess I'd put

    UPDATE table1
    INNER JOIN table2 ON table1.columna=table2.columnd
    SET table1.columnb= (table2.columne - table2.columnf) / (2 + table2.columnf)

    basically whatever is in the brackets will be calculated first, then division and multiplication and finally addition and subtraction.
    If you need the order to be different then you need to add brackets to control it.
     
    sarahk, Oct 14, 2013 IP