SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Ottawa, Ont. Canada
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    IF Statment in an UPDATE Statement - Help needed

    Hello

    I have been working on this problem for hours.

    I would like to use an UPDATE statement with an IF .

    Like this:

    $unit_size is usually 1.000 but it could be less than 1.000 It is never greater than 1.000

    sql_up = "UPDATE table1 SET unit_total = IF (" . $unit_size . " > .999, unit_total = unit_total + 1, unit_total = unit_total + " . $unit_size . ") WHERE unit_id != '" . $unit_id . "' ORDER BY unit_id DESC LIMIT 64";

    It does not store any value in unit_total

    Any help would be appreciated.

    Thank you

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    So what you want is:
    IF (unit_size >= 1) { unitotal++; } ELSE {unit_total += unit_size)....

    why not do
    $unit_size = ($unit_size >= 1) ? 1 : $unit_size;

    before the query, and then just UPDATE table1 SET unit_total = unit_total + ".$unit_size." ....
    ?

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Ottawa, Ont. Canada
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello StarLion

    First of all thank you for responding to my post so quickly! Many times it takes several hours or a couple of days to get a response.

    Regarding your answer:
    IF (unit_size >= 1) { unitotal++; } ELSE {unit_total += unit_size)

    gave me a SQL Error. (Looking at it now, the brackets are not matched up correctly. I just saw that after I tried option 2.)

    But I tried the second option and it worked.

    Thank you


Bookmarks

Posting Permissions

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