Solved

MySQL Edit Syntax

Posted on 2014-01-09
9
278 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

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!

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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