dbTalk Databases Forums  

Quick Question Regarding MYSQL CASE in UPDATE statement

comp.databases.mysql comp.databases.mysql


Discuss Quick Question Regarding MYSQL CASE in UPDATE statement in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
cronoklee
 
Posts: n/a

Default Quick Question Regarding MYSQL CASE in UPDATE statement - 10-30-2010 , 05:41 PM






Hi, Can someone smart please let me know what's wrong with this
statement?

UPDATE table
CASE WHEN type='longvalue' THEN SET longvalue='Results'
ELSE SET value='Results'
END
WHERE id=10

Thanks,
cron

Reply With Quote
  #2  
Old   
Jerry Stuckle
 
Posts: n/a

Default Re: Quick Question Regarding MYSQL CASE in UPDATE statement - 10-30-2010 , 07:42 PM






On 10/30/2010 6:41 PM, cronoklee wrote:
Quote:
Hi, Can someone smart please let me know what's wrong with this
statement?

UPDATE table
CASE WHEN type='longvalue' THEN SET longvalue='Results'
ELSE SET value='Results'
END
WHERE id=10

Thanks,
cron
What's your table definition and what error message do you get?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex (AT) attglobal (DOT) net
==================

Reply With Quote
  #3  
Old   
cronoklee
 
Posts: n/a

Default Re: Quick Question Regarding MYSQL CASE in UPDATE statement - 10-30-2010 , 07:50 PM



On 31 Oct, 01:42, Jerry Stuckle <jstuck... (AT) attglobal (DOT) net> wrote:
Quote:
On 10/30/2010 6:41 PM, cronoklee wrote:

Hi, Can someone smart please let me know what's wrong with this
statement?

UPDATE table
CASE WHEN type='longvalue' THEN SET longvalue='Results'
ELSE SET value='Results'
END
WHERE id=10

Thanks,
cron

What's your table definition and what error message do you get?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck... (AT) attglobal (DOT) net
==================

HI Jerry,

Error is
[Err] 1064 - 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 'CASE WHEN type='longvalue' THEN SET longvalue='Results'
ELSE SET value='Result' at line 2

Table is myISAM
`id` mediumint(9) NOT NULL auto_increment
`type` varchar(20) NOT NULL
`value` varchar(255) default NULL
`longvalue` blob

Thanks,
cron

Reply With Quote
  #4  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Quick Question Regarding MYSQL CASE in UPDATE statement - 10-31-2010 , 12:57 AM



On 2010-10-31 00:41, cronoklee wrote:
Quote:
Hi, Can someone smart please let me know what's wrong with this
statement?

UPDATE table
CASE WHEN type='longvalue' THEN SET longvalue='Results'
ELSE SET value='Results'
END
WHERE id=10

You cannot use a case statement the way you are trying to. Basically you
will have to update a column via a case statement, you cannot choose
column in the case statement:

update table
set longvalue = case when type = 'longvalue'
then 'Results'
else longvalue
end,
value = case ...
where id = 10;

I'd say that the scenario you are describing is rather unusual, is there
something wrong with the design of your database?

/Lennart

Reply With Quote
  #5  
Old   
Jerry Stuckle
 
Posts: n/a

Default Re: Quick Question Regarding MYSQL CASE in UPDATE statement - 10-31-2010 , 07:14 AM



On 10/30/2010 8:50 PM, cronoklee wrote:
Quote:
On 31 Oct, 01:42, Jerry Stuckle<jstuck... (AT) attglobal (DOT) net> wrote:
On 10/30/2010 6:41 PM, cronoklee wrote:

Hi, Can someone smart please let me know what's wrong with this
statement?

UPDATE table
CASE WHEN type='longvalue' THEN SET longvalue='Results'
ELSE SET value='Results'
END
WHERE id=10

Thanks,
cron

What's your table definition and what error message do you get?



HI Jerry,

Error is
[Err] 1064 - 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 'CASE WHEN type='longvalue' THEN SET longvalue='Results'
ELSE SET value='Result' at line 2

Table is myISAM
`id` mediumint(9) NOT NULL auto_increment
`type` varchar(20) NOT NULL
`value` varchar(255) default NULL
`longvalue` blob

Thanks,
cron
OK, so the error message is pointing you at the problem. Where do you
see that you can use CASE like this?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex (AT) attglobal (DOT) net
==================

Reply With Quote
  #6  
Old   
cronoklee
 
Posts: n/a

Default Re: Quick Question Regarding MYSQL CASE in UPDATE statement - 10-31-2010 , 08:09 AM



On 31 Oct, 05:57, Lennart Jonsson <erik.lennart.jons... (AT) gmail (DOT) com>
wrote:
Quote:
On 2010-10-31 00:41, cronoklee wrote:

Hi, Can someone smart please let me know what's wrong with this
statement?

UPDATE table
CASE WHEN type='longvalue' THEN SET longvalue='Results'
ELSE SET value='Results'
END
WHERE id=10

You cannot use a case statement the way you are trying to. Basically you
will have to update a column via a case statement, you cannot choose
column in the case statement:

update table
* * set longvalue = case when type = 'longvalue'
* * * * * * * * * * * * * * * then 'Results'
* * * * * * * * * * * * * * * else longvalue
* * * * * * * * * * end,
* * * * value = case ...
where id = 10;

I'd say that the scenario you are describing is rather unusual, is there
something wrong with the design of your database?

/Lennart


Excellent thanks a lot Lennart! I figured I had the syntax wrong, but
because it's such an unusual query, I had trouble correcting it using
google. I think my table design is okay, it's just a one off oddball
circumstance.
Thanks again for the help!
cron

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.