Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

MySQL Edit Syntax

Posted on 2014-01-09
9
277 Views
Last Modified: 2014-01-09
I have a table called 'charges'

I want to update the value of field 'calculated_weight' based on the contents of 'department_id', which will determine what to multiply against 'quantity' to determine the update value for 'calculated_weight'.  Example:

UPDATE `charges` SET `calculated_weight` = IF(`department_id` IS 4120, 1, 2);

Now, that isn't want I want, but puts into logical form the paragraph above it *almost.  

What I really want is to insert (quantity*.0857) into `calculated_weight` when `department_id` == 4120.
IF
'department_id' == 4250, then i would want (quantity*1.0147) to insert into 'calculated_weight'

I have about 6 departmentIDs to monitor and adjust the mutlplication value for.  

I tried a few options in MySQL, but it isn't working out for me.  

Thanks
0
Comment
Question by:weklica
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 200 total points
ID: 39769004
Why not this for each department and repeat just adjusting the calculation

UPDATE `charges` SET `calculated_weight` = (quantity*.0857) where department_id=4120

edited
For multiple departments then just an OR

UPDATE `charges` SET `calculated_weight` = (quantity*.0857) where department_id=4120 OR department_id=4121 OR department_id=4122
0
 
LVL 13

Accepted Solution

by:
Ashok earned 300 total points
ID: 39769007
Try

UPDATE `charges` SET
`calculated_weight` =
CASE `department_id` WHEN 4120 THEN quantity*.0857
WHEN `department_id` WHEN 4250 THEN quantity*1.0147
....
ELSE 'Charges' END
0
 
LVL 33

Expert Comment

by:Norie
ID: 39769008
Have you tried using CASE..WHEN?

UPDATE `charges`
SET `calculated_weight` =
  CASE
     WHEN `department_id`= 4120 THEN quantity*0.0857
     WHEN `deparment_id` = 4250 THEN quantity*1.0147
      -- etc
  END
0
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 

Author Comment

by:weklica
ID: 39769010
That works perfectly.  I thought I tried something like that, but clearly missed something.  I used this specifically and it worked:

UPDATE  `charges` SET  `calculated_weight` = (  `quantity` * .0857 ) WHERE  `department_id` =4120
0
 

Author Comment

by:weklica
ID: 39769018
I just noticed the CASE when option and will ultimately go wtih that one.  Very much appreciated.  You guys are awesome.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39769019
I did a small edit for doing multiple departments.
0
 
LVL 33

Expert Comment

by:Norie
ID: 39769026
cathal

If you have multiple deparments why not use an IN clause.

`deparment_id` IN (4120, 4121, 4122)
0
 
LVL 13

Expert Comment

by:Ashok
ID: 39769035
I have about 6 departmentIDs to monitor and adjust the mutlplication value for.  

If you are going to update all 6 departments (the whole table), you do not need

where `deparment_id` IN (4120, 4121, 4122)
0
 
LVL 58

Expert Comment

by:Gary
ID: 39769045
Cos I was having a dumb blonde moment.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question