If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > IF in Update query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-27-11, 14:48
Muiter Muiter is offline
Registered User
 
Join Date: Dec 2008
Location: Netherlands
Posts: 56
Question 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'
Reply With Quote
  #2 (permalink)  
Old 03-27-11, 16:27
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-28-11, 08:39
Muiter Muiter is offline
Registered User
 
Join Date: Dec 2008
Location: Netherlands
Posts: 56
Ok, thx for pointing this out.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On