How to update multiple rows value in one query in particular column - mysql

UPDATE job_seekers SET js_email =
CASE js_email
WHEN "Wbsirota#gmail.com" THEN "bsirota#gmail.com"
WHEN "#nautiyalanuj#gmail.com"THEN "nautiyalanuj#gmail.com"
WHEN ".agiletestanalyst​#gmail.com" THEN "agiletestanalyst​#gmail.com"
END ) WHERE js_email IN ("wbsirota#gmail.com",
"#nautiyalanuj#gmail.com",
".agiletestanalyst​#gmail.com");

UPDATE
job_seekers
SET
js_email = CASE WHEN js_email = 'Wbsirota#gmail.com' THEN
'bsirota#gmail.com' WHEN js_email = '#nautiyalanuj#gmail.com' THEN
'nautiyalanuj#gmail.com' WHEN js_email = '.agiletestanalyst​
#gmail.com' THEN 'agiletestanalyst​#gmail.com' ELSE js_email END
WHERE
js_email IN ("wbsirota#gmail.com","#nautiyalanuj#gmail.com",
".agiletestanalyst​#gmail.com");
EXAMPLE IS BELOW
UPDATE
dbo.TestStudents
SET
LASTNAME = CASE WHEN LASTNAME = 'AAA' THEN 'BBB'
WHEN LASTNAME = 'CCC' THEN 'DDD'
WHEN LASTNAME = 'EEE' THEN 'FFF' ELSE LASTNAME END
WHERE
LASTNAME IN ('AAA', 'CCC', 'EEE');

Related

error code 1048 column 'value' cannot be null

i have SP for update the data into new data with this query, so basically every id have own value for own rule_id, and this SP is to update the data with the new one from the SP
CREATE DEFINER=`root`#`localhost` PROCEDURE `update_discount_campaign_advanced2`(IN DiscountCampaignId INT(10),
IN rule_percentage VARCHAR(255),
IN rule_quota_daily_max VARCHAR(255),
IN rule_discount_daily VARCHAR(255),
IN rule_discount_weekly VARCHAR (255),
IN rule_discount_monthly VARCHAR(255),
IN rule_spesific_payment_method VARCHAR(255),
IN rule_spesific_cc_operator VARCHAR(255),
IN allow_other_product VARCHAR(255)
)
proc:
BEGIN
DECLARE is_discount_campaign_id INT(10);
DECLARE is_rule_percentage VARCHAR(255);
DECLARE is_rule_quota_daily_max VARCHAR(255);
DECLARE is_rule_discount_daily VARCHAR(255);
DECLARE is_rule_discount_weekly VARCHAR(255);
DECLARE is_rule_discount_monthly VARCHAR(255);
DECLARE is_rule_spesific_payment_method VARCHAR(255);
DECLARE is_rule_spesific_cc_operator VARCHAR(255);
DECLARE is_allow_other_product VARCHAR(255);
DECLARE param_1_message TEXT;
SET #is_discount_campaign_id = DiscountCampaignId;
SET #is_rule_percentage = rule_percentage;
SET #is_rule_quota_daily_max = rule_quota_daily_max;
SET #is_rule_discount_daily = rule_discount_daily;
SET #is_rule_discount_weekly = rule_discount_weekly;
SET #is_rule_discount_monthly = rule_discount_monthly;
SET #is_rule_spesific_payment_method = rule_spesific_payment_method;
SET #is_rule_spesific_cc_operator = rule_spesific_cc_operator;
SET #is_allow_other_product = allow_other_product;
SELECT CONVERT( CONCAT('Maaf, ID discount_campaign tidak bisa \'0\'') USING UTF8) INTO param_1_message;
IF (DiscountCampaignId = 0) THEN
SELECT param_1_message AS message;
LEAVE proc;
END IF;
IF (DiscountCampaignId != 0) THEN
INSERT INTO discount_campaign_advanced (id,discount_campaign_id,
discount_advanced_rules_id, value, createdAt, status, updatedAt)
select *, CASE WHEN
unitData.id is NULL THEN NULL ELSE NOW() END as updatedAt from (SELECT dca.id , DiscountCampaignId, dar.id as dar_id1, CASE WHEN dar.id = 1
THEN #is_rule_percentage WHEN dar.id = 2 THEN #is_rule_quota_daily_max
WHEN dar.id = 3 THEN #is_rule_discount_daily WHEN dar.id = 4
THEN #is_rule_discount_weekly WHEN dar.id = 5 THEN #is_rule_discount_monthly
WHEN dar.id = 6 THEN #is_rule_spesific_payment_method WHEN dar.id = 7
THEN #is_rule_spesific_cc_operator END, coalesce(dca.createdAt, NOW()), CASE when (dar.id = 1
and #is_rule_percentage != '0') THEN 1 WHEN (dar.id = 2 and
#is_rule_quota_daily_max != '0') THEN 1 WHEN
(dar.id = 3 and #is_rule_discount_daily != '0') THEN 1 WHEN (dar.id = 4 and
#is_rule_discount_weekly != '0') THEN 1
WHEN (dar.id = 5 AND #is_rule_discount_monthly != '0') THEN 1 WHEN (dar.id = 6
AND #is_rule_spesific_payment_method != '0')
THEN 1 WHEN (dar.id = 7 AND #is_rule_spesific_cc_operator != '0') THEN 1 ELSE 0 END as bb
FROM
discount_advanced_rules dar
LEFT JOIN
discount_campaign_advanced dca ON dca.discount_advanced_rules_id = dar.id
and dca.discount_campaign_id = DiscountCampaignId) as unitData
WHERE unitData.bb = 1
UNION
SELECT dca.id, DiscountCampaignId, dca.discount_advanced_rules_id, CASE WHEN dca.discount_advanced_rules_id = 1
THEN #is_rule_percentage WHEN dca.discount_advanced_rules_id = 2 THEN #is_rule_quota_daily_max
WHEN dca.discount_advanced_rules_id = 3 THEN #is_rule_discount_daily WHEN dca.discount_advanced_rules_id = 4
THEN #is_rule_discount_weekly WHEN dca.discount_advanced_rules_id = 5 THEN #is_rule_discount_monthly
WHEN dca.discount_advanced_rules_id = 6 THEN #is_rule_spesific_payment_method WHEN dca.discount_advanced_rules_id = 7
THEN #is_rule_spesific_cc_operator END, coalesce(dca.createdAt, NOW()), CASE when (dca.discount_advanced_rules_id = 1
and #is_rule_percentage != '0') THEN 1 WHEN (dca.discount_advanced_rules_id = 2 and
#is_rule_quota_daily_max != '0') THEN 1 WHEN
(dca.discount_advanced_rules_id = 3 and #is_rule_discount_daily != '0')
THEN 1 WHEN (dca.discount_advanced_rules_id = 4 and
#is_rule_discount_weekly != '0') THEN 1
WHEN (dca.discount_advanced_rules_id = 5
AND #is_rule_discount_monthly != '0') THEN 1 WHEN (dca.discount_advanced_rules_id = 6
AND #is_rule_spesific_payment_method != '0')
THEN 1 WHEN (dca.discount_advanced_rules_id = 7 AND #is_rule_spesific_cc_operator != '0') THEN 1 ELSE 0 END as bb,
NOW()
FROM discount_campaign_advanced dca
WHERE dca.discount_campaign_id = DiscountCampaignId
on duplicate key update id = values(id), discount_campaign_id = values(discount_campaign_id),
discount_advanced_rules_id = values(discount_advanced_rules_id), value = values(value),
createdAt = values(createdAt), status = values(status), updatedAt = values(updatedAt);
END IF;
IF (allow_other_product != 0) THEN
INSERT INTO discount_campaign_advanced (id,discount_campaign_id,
discount_advanced_rules_id, value, createdAt, status, updatedAt)
select *, CASE WHEN
unitData.id is NULL THEN NULL ELSE NOW() END as updatedAt from (SELECT dca.id , DiscountCampaignId, dar.id as dar_id1, CASE WHEN dar.id = 8
THEN #is_allow_other_product END, coalesce(dca.createdAt, NOW()), CASE when (dar.id = 8
and #is_allow_other_product != '0') THEN 1 ELSE 0 END as bb
FROM
discount_advanced_rules dar
LEFT JOIN
discount_campaign_advanced dca ON dca.discount_advanced_rules_id = dar.id
and dca.discount_campaign_id = DiscountCampaignId) as unitData
WHERE unitData.bb = 1
UNION
SELECT dca.id, DiscountCampaignId, dca.discount_advanced_rules_id, CASE WHEN dca.discount_advanced_rules_id = 8 THEN #is_allow_other_product END, coalesce(dca.createdAt, NOW()), CASE when (dca.discount_advanced_rules_id = 8
and #is_allow_other_product != '0') THEN 1 ELSE 0 END as bb,
NOW()
FROM discount_campaign_advanced dca
WHERE dca.discount_campaign_id = DiscountCampaignId
on duplicate key update id = values(id), discount_campaign_id = values(discount_campaign_id),
discount_advanced_rules_id = values(discount_advanced_rules_id), value = values(value),
createdAt = values(createdAt), status = values(status), updatedAt = values(updatedAt);
END IF;
END
but idk why when i call this SP call brambang_uom.update_discount_campaign_advanced2(2757, '5', '5', '1', '1', '1', '1', '1', '9');
it returns Error Code: 1048. Column 'value' cannot be null`, where my wrong at?

sum function based on unique date

actually i want a total value based on unique date
SELECT t2.stateName,
t3.`districtName`,t1.fingerDate,COUNT(a.arg1),COUNT(a.arg2),COUNT(a.arg3),COUNT(a.arg4),COUNT(a.arg5),COUNT(a.arg6),COUNT(a.arg7), COUNT(a.arg8),(COUNT(a.arg1) + COUNT(a.arg2) + COUNT(a.arg3) + COUNT(a.arg4) + COUNT(a.arg5) + COUNT(a.arg6) + COUNT(a.arg7))AtotalScreeningArg
FROM tbl_user AS t1
LEFT JOIN tbl_state AS t2 ON t1.addressState = t2.stateId
LEFT JOIN `tbl_district` AS t3 ON t1.addressDistrict = t3.`districtId`
LEFT JOIN (SELECT userId,
CASE WHEN occupation = "Truckers" OR occupation = "Drivers"
THEN occupation END arg1,
CASE WHEN (occupation = "Migrant")
THEN occupation END arg2,
CASE WHEN (hrg IS NULL OR hrg = " ") AND (`arg` IS NULL OR `arg` = " ")
THEN "Student" END arg3,
CASE WHEN (occupation = "Daily Wage")
THEN occupation END arg4,
CASE WHEN (occupation = "Salaried" OR occupation = "self Employed" OR occupation ="Unemployed" OR occupation = "Other")
THEN occupation END arg5,
CASE WHEN (ARG = "Female Partner (FPHRG)" OR ARG = "Partner / Spouse of FSW")
THEN ARG END arg6,CASE WHEN ARG = "Female Partner (FPARG)" THEN ARG END arg7,
CASE WHEN ARG = "TG (F-M)" THEN ARG END arg8,CASE WHEN hrg = "MSM" THEN hrg END arg9,
CASE WHEN hrg = "TG(M_F)" THEN hrg END arg10,CASE WHEN hrg = "FSW" THEN hrg END arg11,CASE WHEN hrg = "IDU" THEN hrg END arg12 FROM `tbl_user`
WHERE fingerDate IS NOT NULL OR fingerDate != " ")a ON t1.userId = a.userId
WHERE `t1`.`deleted` = "N" AND t1.userType = "user" AND `t1`.`fingerDate` >= '2018-11-01' AND `t1`.`fingerDate` <= '2018-12-01' AND t1.addressState = '34' AND t1.addressDistrict = '614' GROUP BY t1.userId
it gives result but not for unique date

CASE statement not updating Temp Table

In the UPDATE statement below, the InspectionChg, MileageChg, FuelChg and FreightChg columns are not updating to temp table. Goal is to aggregate charges. I have tried several variations and I cannot get Temp Table Columns to update.
UPDATE #TTable
SET ChargeCode = id.cht_itemcode,
InspectionChg = CASE WHEN LEFT(id.cht_itemcode, 3) = 'INS' THEN
InspectionChg+id.ivd_charge ELSE InspectionChg+0 END,
MileageChg = CASE WHEN LEFT(id.cht_itemcode, 4) = 'MILE' THEN MileageChg+id.ivd_charge
ELSE MileageChg+0 END,
FuelChg = CASE WHEN LEFT(id.cht_itemcode, 4) = 'FUEL' THEN
FuelChg+id.ivd_charge ELSE FuelChg+0 END,
FreightChg = CASE WHEN LEFT(id.cht_itemcode, 2) = 'LH' THEN FreightChg+id.ivd_charge
ELSE FreightChg+0 END,
Rate = 1
FROM #TTable
INNER JOIN invoicedetail as id
on #TTable.OrderNumber = id.Ord_hdrnumber
What is wrong?
UPDATE TT
SET TT.ChargeCode = id.cht_itemcode ,
TT.InspectionChg = ( CASE WHEN LEFT(id.cht_itemcode, 3) = 'INS'
THEN InspectionChg + id.ivd_charge
ELSE InspectionChg + 0
END ) ,
TT.MileageChg = ( CASE WHEN LEFT(id.cht_itemcode, 4) = 'MILE'
THEN MileageChg + id.ivd_charge
ELSE MileageChg + 0
END ) ,
TT.FuelChg = ( CASE WHEN LEFT(id.cht_itemcode, 4) = 'FUEL'
THEN FuelChg + id.ivd_charge
ELSE FuelChg + 0
END ) ,
TT.FreightChg = ( CASE WHEN LEFT(id.cht_itemcode, 2) = 'LH'
THEN FreightChg + id.ivd_charge
ELSE FreightChg + 0
END ) ,
TT.Rate = 1
FROM #TTable AS TT
INNER JOIN invoicedetail AS id ON TT.OrderNumber = id.Ord_hdrnumber

Query using CASE WHEN

I am trying to write a query like this and I am getting an error. It is my first time using case, so that's where I believe the problem to be.
UPDATE my_table
CASE
WHEN downloads IS NULL THEN
SET downloads = 1
ELSE
SET downloads + 1
END
WHERE attachment_id = 8990
AND parent_post_id = 9221
OR attachment_id = 9211
AND parent_post_id = 383
You can rewrite it as below
UPDATE my_table
SET downloads = CASE WHEN downloads IS NULL THEN 1
ELSE downloads + 1 END
WHERE attachment_id = 8990
AND (parent_post_id = 9221
OR attachment_id = 9211 )
AND parent_post_id = 383
Also you need to group your or condition in () in order to match 9211 against parent_post_id and attachment_id with or operation,also there is confusing conditions you have in your query how parent_post_id and attachment_id can be equal to 2 values at same time may be you are looking for
WHERE (attachment_id = 8990 AND parent_post_id = 9221 )
OR (attachment_id = 9211 AND parent_post_id = 383)
updates syntax is update table set col=value where condition. Using case doesn't change that. case can only be used to return an expression, so:
UPDATE my_table
SET downloads = CASE WHEN downloads IS NULL THEN 1 ELSE downloads + 1 END
WHERE attachment_id = 8990 AND
parent_post_id = 9221 OR
attachment_id = 9211 AND
parent_post_id = 383

Call procedure in mysql

I have a table , that store friendship like :
Friend : f_id , f_userid1 , f_userid2 , f_status .
If user a is friend with user b , I only insert one row :
f_userid1 = a , f_userid2 = b
I want to check if I have this friendship row , change the status to zero , I have created a procedure for it :
BEGIN
DECLARE fid INT(15);
-- check for first one
SELECT f_id INTO fid FROM f WHERE
f_userid1 = #userid1
AND f_userid2 = #userid2;
SET oup = 1;
IF(fid = 0) THEN
SET oup = 2;
-- check for SECOUND one
SELECT f_id INTO fid FROM f WHERE f_userid2 = #userid1 AND f_userid1 = #userid2;
END IF;
-- if statment
IF(fid > 0) THEN
SET oup = 3;
-- we need to update the exiting request
UPDATE f SET f_userid1 = #userid1 , f_userid2 = #userid2 , f_status = 0 WHERE f_id = #fid ;
END IF;
END
I have a row like here before
f_id = 1 , f_userid1 = a . f_userid2 = b , f_status = 1;
It runs successfully , but no row affacted !
I have added output and It only return 1 ;
where is my problem ?
Why not write it in a single query?
UPDATE f
SET f_status = 0
WHERE ( f_userid1 = #userid1 AND f_userid2 = #userid2 )
OR ( f_userid1 = #userid2 AND f_userid2 = #userid1 );