Kahuki Webmaster Forum and Discussion Community  

Go Back   Kahuki Webmaster Forum and Discussion Community > Website Development & Management > Programming > PHP



Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-15-2007, 04:54 PM
Member
 
Join Date: Mar 2006
Posts: 65
kreak is on a distinguished road
MySQL columns and an upper limit.?

I want to update a table with a few thousands rows of data in it. I want to increment one column by another column but I want the result to have a hardcoded upper limit... I can do this easily with two queries, but I want to do it in one... Something like :

" update [database].[table] set [column a]=[column a]+[column b];"
" update [database].[table] set [column a]=[upper limit] where [column a]>[upper limit];"

As a single query... If anyone can help me out there's some juicy stars waiting for you.

Thanks in advance.
dhvrm :

Perfect, I'm usually quite good at finding things like this out myself but the MySQL manual and my brain just don't mesh well together...

I'd 5 star you now, but I've got to wait a bit.

Thanks!
Oooo.. Actually after testing out the initial answer, it appears the second may be the more accurate one.

Let's see when the timer expires, thanks for the information!
Well, so far neither of these actually get the result I expected... Which is weird... I'm not actually doing a column_a+column_b at the moment, but I will do in the future, but I presumed it would be the same for a value..?

Here's the actual Python code I am executing.


self.cursor.execute('update '+DB_NAME+'.acc
SET stamina=CASE
WHEN col_a+'+str(float(self.parent.CFG[0]["col_a_increase"]))+' > '+str(100)+' THEN 100
ELSE col_a=col_a+'+str(float(self.parent.CFG[0]["col_a_increase"]))+'
END;')

I'm sure that'll look horrible on here, but it's a basic representation of the code... Where I've got str(100) I know I could just hardcode 100 in to the query, but later on that's going to be flexible...

Anyway, I think I might be able to sort this out based on the info you both gave, thanks again! If you spot something I've done wrong then I appreciate the additional, additional assistance

*Grin*

__________________
Big Web Links Directory

Reply With Quote
  #2 (permalink)  
Old 01-15-2007, 05:18 PM
Member
 
Join Date: Feb 2006
Posts: 59
martin is on a distinguished road
UPDATE table
SET columnA = SELECT CASE
WHEN columnA + columnB > limit THEN limit
ELSE columnA + columnB
END

UPDATE:

Yes, you need to take the SELECT out of my original code.

UPDATE table
SET columnA = CASE
WHEN columnA + columnB > limit THEN limit
ELSE columnA + columnB
END

Reply With Quote
  #3 (permalink)  
Old 01-15-2007, 05:22 PM
Rookie
 
Join Date: Feb 2007
Posts: 2
esedge is on a distinguished road
Hey there.

if you have MySQL 5.0 this query should do.

You can control the flow of your query using Control Flow Functoins:
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html
In this case the Case statement in Mysql is almost like the SQL Server one.

UPDATE [database].[table] SET [column a]=
CASE WHEN [column a]+[column b] > [upper limit] THEN [column a]+[column b]
ELSE [upper limit] END

Hope it helps.

Reply With Quote
Reply

Bookmarks

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL & MySQL Server Removal Help Needed Please? I feel so dumb to be this damn smart!? blackfox PHP 1 11-26-2006 06:27 PM
MYSQL max queries limit ? bakul jyoshi PHP 3 06-29-2006 10:40 PM
how to limit hosting periods in Cpanel (like 2/4 months)? prevo Web Hosting 2 06-19-2006 06:11 PM
how do limit the connections from a user to my apache server? sstok Web Hosting 3 05-16-2006 12:21 PM
MySQL, Can you get the sum of two columns in the same row? Violent770 PHP 2 03-07-2006 06:35 PM


All times are GMT. The time now is 09:52 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0