Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL Edit Syntax

Posted on 2014-01-09
9
Medium Priority
?
282 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
[X]
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
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 800 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 1200 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 35

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 35

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

604 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