update case query for multiple conditions - mysql

I am using an UPDATE query to update multiple rows which is something like the following
UPDATE TBLNAME SET NEWCOLUMN =
CASE
WHEN TYPE='ACCOUNT' THEN 'FINANCE'
WHEN CODE='DATA' AND CLASS='FIRST' THEN 'LEGAL'
END
The above query works fine for single conditions and for certain conditions something like
NEWCOLUMN = Audit when Type = file, fax, documents, I was using something like
UPDATE TBLNAME SET NEWCOLUMN =
CASE
WHEN TYPE='ACCOUNT' THEN 'FINANCE'
WHEN CODE='DATA' AND CLASS='FIRST' THEN 'LEGAL'
WHEN TYPE='FILE' AND 'FAX' AND 'DOCUMENTS' THEN 'AUDIT'
END
The above query works fine for the first two conditions but the third condition AUDIT is not updated.
Could somebody help?
Thanks

Use
WHEN TYPE IN ('FILE','FAX','DOCUMENTS') THEN 'AUDIT' END

Related

MYSQL - How to run a query where the values of one condition match the value of another?

It would be something where you could select eye_color, hair_color, sex...from a table of characteristics and if every value of eye_color(e.g, 'Dark brown') = every hair_color('Dark brown') then you run the query.
I finally found the solution for MySQL:
SELECT eye_color=hair_color, sex...
FROM characteristics
WHERE eye_color=hair_color = '1';

Pin/Flag a record in a mysql table to true and the rest to false in a single statement

Address Table with 50 records. Has a bool field called "primary_address_indicator".
I want to update a selected record to true and automatically set all other 49 records to false.
I know I can do this with 2 sql statements
First (update all records to false)
UPDATE address SET address.primary_address_indicator=0
Then (update specific record to true)
UPDATE address SET address.primary_address_indicator=1 WHERE address.record_id=31
Is there a way to do this in a single sql statement? Something like #Rank?
Keeping it simple... no Unions or some weird self table join.
Maybe what I am looking for does not exist...and that is fine too. just curious.
Update with Case
UPDATE tableName
SET Pin = CASE
WHEN 1=1 --'your condition'
THEN True
ELSE False
END
PC : #keWalker
The simplest way is simply to treat the boolean result as a number:
UPDATE address a
SET a.primary_address_indicator = (a.record_id = 31);
CASE is the formal way to do it. This short-hand is specific to MySQL but it is one of the database-specific features that I like.

MySQL Stored Procedure update using a Join and CASE..ELSE syntax

This is my first time creating a MySQL stored procedure and am stuck on getting the UPDATE piece to work correctly. The proc is performing an inner join, looking for matches on a domain name field. If there is a match, a column named inbound is getting updated with a value of 0. If there is not a match on the join, then I need inbound set to a value of 1.
When I run this, I am able to get the matches tagged with a 0, but the non-matches are not getting updated with a 1. I thought how I have the 'ELSE' part set up would take care of this- can anyone tell if I am missing something with the syntax?
CREATE PROCEDURE `sp_InboungTagging`()
BEGIN
update `tableA` a
inner join `TableD` d
on a.senderDomain = d.domainName
set inbound = CASE
when a.senderDomain = d.domainName then 0
ELSE 1
END
WHERE inbound is null;
END;|
DELIMITER ;
Thanks,
Ron
EDIT-
Thanks for your reply. I am looking for exact matches on a varchar field that has domain names in it- the master list of domains is in table D. If the record in TableA has a match in TableD, I want to tag that recored with a 0. If there is no match in TableD, then I would like to tag it with a 1. Let me know if that clears things up- thanks
Your JOIN condition is the same as your CASE condition. If you JOIN your two tables on:
a.senderDomain = d.domainName
Then there will be no values in the result set for which
a.senderDomain != d.domainName
so the ELSE clause of your CASE statement never fires.
Without knowing more about what you mean by "matches" and "non-matches," I can't really suggest a correction.

Select column to update based on value

What I am trying to do is reduce the time needed to aggregate data by producing a roll-up table of sorts. When I insert a record, an after insert trigger is fired which will update the correct row. I would update all of the columns of the roll-up table if I need to, but since there are 25 columns in the table and each insert will only update 2 of them, I would rather be able to dynamically select the columns to update. My current update statement in the after insert trigger looks similar to this:
update peek_at_chu.organization_data_state_log odsl
inner join ( select
lookUpID as org_data_lookup,
i.interval_id,
peek_at_chu.Get_Time_Durration_In_Interval1('s', new.start_time, new.end_time, i.start_time, i.end_time) as time_in_int,
new.phone_state_id
from
(peek_at_chu.interval_info i
join peek_at_chu.interval_step int_s on i.interval_step_id = int_s.interval_step_id)) as usl on odsl.org_date_lookup_id = usl.org_data_lookup
and odsl.interval_id = usl.interval_id
set
total_seconds = total_seconds + usl.time_in_int,
case new.phone_state_id
when 2 then
available_seconds = available_seconds + time_in_int
end;
In this, lookUpID is a variable previously declared in the trigger. The field that will dictate which field of the roll-up table to update is new.phone_state_id. The phone_state_id's are not consistent, that is some numbers are skipped in this table, so an update based on column number is out the window unless I create a mapping.
The case option throws an error but I am hoping to use something similar to that instead of 25 if statements if I can.
You have to update all the columns, but use a conditional to determine whether to give it a new value or keep the old value:
set total_seconds = total_seconds + usl.time_in_int,
available_seconds = IF(new.phone_state_id = 2, available_seconds + time_in_int, available_seconds)
Repeat the pattern in the last line for all the other columns that need to be updated conditionally.

getting the updated value from an UPDATE query

I have an UPDATE Query :
UPDATE FKMS_GNST_Transaction_Details
SET Received_Quantity=Received_Quantity+(
CASE
WHEN (#int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity)
THEN GTD.Quantity-GTD.Received_Quantity
ELSE (#int_Updated_Qty)
END)
,#int_GNST_Reference_Id=GTD.Transaction_Detail_Id
FROM FKMS_GNST_Transaction_Details GTD
INNER JOIN #tbl_transactions tmp
ON tmp.Transaction_id=GTD.Transaction_id
AND GTD.Item_id=tmp.Item_id
I want to get the quantity which is added to the Received_Quantity field. That is, if (#int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity) then GTD.Quantity-GTD.Received_Quantity other wise #int_Updated_Qty.
How can we take this value (into a variable or any other way)? Please help.
Use the OUTPUT clause
UPDATE FKMS_GNST_Transaction_Details
SET Received_Quantity=Received_Quantity+(
CASE
WHEN (#int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity)
THEN GTD.Quantity-GTD.Received_Quantity
ELSE (#int_Updated_Qty)
END)
,#int_GNST_Reference_Id=GTD.Transaction_Detail_Id
--start gbn code
OUTPUT INSERTED.Received_Quantity
--end gbn code
FROM FKMS_GNST_Transaction_Details GTD
INNER JOIN #tbl_transactions tmp
ON tmp.Transaction_id=GTD.Transaction_id
AND GTD.Item_id=tmp.Item_id
The OUTPUT results can go
into a table (real, temp or variable)
to the client as a recordset
You can't assigned directly to a local variable