Solved

MySQL Edit Syntax

Posted on 2014-01-09
9
274 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
 

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
[MYSQL]: Delete is very slow 4 54
Apache/PHP: Cannot make phpinfo() and phpmyadmin work 4 37
update joined tables 2 31
email about the whoisactive result 7 27
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Creating and Managing Databases with phpMyAdmin in cPanel.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now