Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2008
    Location
    Netherlands
    Posts
    58

    Question Unanswered: IF in Update query

    Can I use an IF in my UPDATE statement?

    PHP Code:
    $sql_inh "UPDATE planning_inh_pr SET
                snijden_aantal_gereed = '"
    .$aantal_gereed[$i]."',
                IF('"
    .$aantal[$i]."' = '".$aantal_gereed[$i]."' AND snijden_door = 0, snijden_door = '".$_SESSION['user_id']."'), '')
                WHERE id = '"
    .$i_id[$i]."' "
    Gives an error:
    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 'IF('10' = '5' AND snijden_door = 0, snijden_door = '1'), '') WHERE i' at line 3
    In query: UPDATE planning_inh_pr SET snijden_aantal_gereed = '5', IF('10' = '5' AND snijden_door = 0, snijden_door = '1'), '') WHERE id = '151'

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by Muiter View Post
    Can I use an IF in my UPDATE statement?[/I]
    of course you can, but not like that

    syntax:
    Code:
    UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
        SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
        [WHERE where_condition] ...
    you are allowed to SET multiple column values, but it must always be a column name on the left side of the equation

    i.e. you cannot put the IF on the left side
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Dec 2008
    Location
    Netherlands
    Posts
    58
    Ok, thx for pointing this out.

Posting Permissions

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