How to query a condition on a single object of sql record?

57 Views Asked by At

I'll try my best to explain the situation here. We are using Maria DB.

In certain case we are looping over all the records/rows of a REQUESTS table. and for each object/record we are checking few conditions and if condition passes we are performing an action. All the said things here are being done using ruby. Say I want to check the same on pure SQL, I wanted to see how.

Sample Object a Request table:

[{"ot_rqst_id":27460354,"rqst_type_cd":"NONTP","svc_type_cd":"TD","sl_ttl_type_cd":"","lot_num":41843022, ttl_stg_cd: 'CMPLT'}]

What I want to check on sql: To see if the ttl_stg_cd of this object is equal to 'CMPLT'. If it is then update it to NULL else "something_else".

Let me remind you again that, I am already doing this in ruby language, but I am not an expert on sql so help would be appreciated.

1

There are 1 best solutions below

0
On

The following query should do what you want.
I have taken the id from your query string. You need to put in the real table name and check the WHERE clause. If you remove the WHERE clause it will update all records in the table, you may wish the change it to something like
WHERE column_X = value_Y

UPDATE table_name 
SET ttl_stg_cd = CASE ttl_stg_cd
    WHEN 'CMPLT' THEN null
    ELSE 'something_else' END
WHERE ot_rqst_id = 27460354;