I have this SQL commands, which I would like to run in one command. But if I remove the semicolon from between them, then it doesn't works anymore;
UPDATE runners SET money=20000
WHERE rrank >= 3;
UPDATE runners SET money=25000
WHERE nev = 'Master';
Combine the logic into a single update:
UPDATE runners
SET money = (case when nev = 'Master' then 25000
else 20000
end)
WHERE rrank >= 3 or nev = 'Master';
Partial (however generalized) solution (if runners is not that big table)
update runners
set money = case
when rrank >= 3 then
20000
when nev = 'Master' then
25000
-- any number of when here
-- if no when applied, do nothing
else
money
end
Use a case
UPDATE runners
SET money = case when rrank >= 3
then 20000
when nev = 'Master'
then 25000
end
WHERE rrank >= 3
or nev = 'Master'
Related
So I've been working on a new project. I understand how to update a certain rows column value:
UPDATE PlayerInfo SET GearLevel = GearLevel +1 WHERE UID = "76561198008596823" ;
But I want to add some checks in it.
In Words.
IF UID = "76561198008596823" AND BankMoney = 25000000 AND GearLevel =
15 THEN GearLevel + 1 AND BankMoney - 15000000
So I tested with:
UPDATE PlayerInfo SET GearLevel =
CASE WHEN GearLevel = 15 THEN GearLevel +1
Else
0
END ;
But this just added 1 to all values in the column that was at 15.
How can I add the checks to the CASE and also subtract BankMoney? Or is there an easier way to achieve this?
You may not need to use a CASE expression here:
UPDATE PlayerInfo
SET
GearLevel = GearLevel + 1,
BankMoney = BankMoney - 15000000
WHERE
UID = '76561198008596823' AND
BankMoney = 25000000 AND
GearLevel = 15;
A CASE expression would be needed if you wanted to update every record with some value. Then, its logic would let you carefully decide which records get updated. But if you only want to target accounts with the three conditions you gave us (UID=7656..., BankMoney=2500000 and GearLevel=15), then you may just add this directly to the WHERE clause.
I'm learning to use mysql so i created a really simple project manager program.
i wanted to track if a user saw her/his tasks in a project. So i want to update user_seen_task filed when a project is got queried by a user, but obviously i only want to update this field if it isn't already set.
This is my approach what i have already:
UPDATE task
SET user_seen_task = 1, user_seen_task_date = NOW()
WHERE project_id = 14 AND user = 4 AND user_seen_job != 1
Unfortunately this query will update user_seen_task_date every time it runs.
Any help is greatly appreciated.
UPDATE task
SET user_seen_task = 1
, user_seen_task_date = CASE
WHEN user_seen_task_date IS NULL THEN NOW()
ELSE user_seen_task_date
END
WHERE project_id = 14
AND user = 4
AND user_seen_job != 1
Or if it doesn't need to update user_seen_task in this case, just add
AND user_seen_task_date IS NULL
into your where clause
UPDATE task
SET user_seen_task = 1, user_seen_task_date = NOW()
WHERE project_id = 14 AND user = 4 AND user_seen_job != 1
AND user_seen_task_date IS NULL
I have a table named chart with two columns, named UPC_REPORT_ID and UPC_FLAG
I am trying to prepare a query to update a UPC_FLAG=1 if UPC_REPORT_ID=1 and simultaneously I want to update UPC_FLAG=0 where UPC_REPORT_ID !=2
Please give me the suggestions
UPDATE chart SET UPC_FLAG = (CASE WHEN UPC_REPORT_ID=1 THEN 1 ELSE CASE WHEN UPC_REPORT_ID !=2 THEN 0 END END)
UPDATE chart
SET UPC_FLAG = CASE UPC_REPORT_ID
WHEN 1 THEN 1
WHEN 2 then UPC_FLAG
ELSE 0
END
Use WHEN
UPDATE ... set UPC_FLAG=CASE WHEN UPC_REPORT=1 THEN 1 WHEN UPC_REPORT_ID <> 2 THEN 0 END WHERE...
--dmg
The same thing with IF AS Barmar's answer
UPDATE chart
SET UPC_FLAG = IF(UPC_REPORT_ID = 1,1,IF(UPC_REPORT_ID <> 2,2,0))
I have 7 SQL Query to do a task like this :
1. UPDATE Customer SET CustomerService = 'perta' WHERE FirstName = 'john';
2. UPDATE Customer SET Flag = 1 WHERE OrderNum BETWEEN 2 AND 29;
3. UPDATE Customer SET PurchaseNum = PurchaseNum + 60 WHERE OrderNum BETWEEN 2 AND 29;
4. UPDATE Customer SET OrderNum = OrderNum + 60 WHERE OrderNum BETWEEN 2 AND 29;
5. UPDATE Customer SET PurchaseNum = PurchaseNum - 28 WHERE (PurchaseNum > 29 AND PurchaseNum <= 89) AND (Flag <> 1);
6. UPDATE Customer SET OrderNum = OrderNum - 28 WHERE (OrderNum > 29 AND OrderNum <= 89) AND (Flag <> 1);
7. UPDATE Customer SET Flag = 0 WHERE OrderNum BETWEEN 62 AND 89;
is it possible to 'compress' those SQL Queries into 1 Query?
because I'm afraid that user cancel the process by pressing the ESC button (after he/she press the SUBMIT button), those sequence will be broken in the middle and my table will be messy as well.
If you are using MySQL then see this link it shows you how to implement transaction in MySQL.
http://dev.mysql.com/doc/refman/5.5/en/commit.html
Transaction helps us in many scenario and you are handling one of them where you want to update multiple database tables.
Our current mysql script that connects our Invoicing software to our website updates stock levels and what not, but there is a field in our products table which dictates if the product is visible or not which the script does not address. I want to introduce some IF logic to set the prodvisible column to 1 IF the stock level it's being updated with is > 0.
In my research, it appears that IF's cannot appear outwith functions, sadly something I have no experience of and despite my best efforts I can't get it to work.
The current script we have which works succesfully to update stock levels is as follows...
update isc_products p
set
p.prodcurrentinv =[{Level_LessOrderBook}]
where p.prodcode = '[{ItemNumber}]' and p.prodinvtrack=1
--GO;--
update isc_product_variation_combinations pvc
set
pvc.vcstock = [{Level_LessOrderBook}]
where pvc.vcsku='[{ItemNumber}]'
I want to integrate something into the first section that does something like the following
If [{Level_LessOrderBook}] > 0
p.prodvisible = 1 where p.prodcode = '[{ItemNumber}]'
ENDIF
I don't want it to set the product to invisible if it it's out of stock, just visible if it's in stock.
Thanks for any help.
You should be able to do this without an IF statement:
update isc_products p
set p.prodvisible = 1
where p.prodcode = '[{ItemNumber}]'
and [{Level_LessOrderBook}] > 0
Or, if you were asking about doing it in one statement:
update isc_products p
set
p.prodcurrentinv = [{Level_LessOrderBook}],
p.prodvisible = IF([{Level_LessOrderBook}] > 0, 1, p.prodvisible)
where p.prodcode = '[{ItemNumber}]' and p.prodinvtrack=1
Finally, how about this?
update
isc_products p
set
p.prodcurrentinv = [{Level_LessOrderBook}],
p.prodvisible = case when [{Level_LessOrderBook}] > 0 then 1 else p.prodvisible end
where
p.prodcode = '[{ItemNumber}]'
and p.prodinvtrack = 1
--GO;--
update
isc_product_variation_combinations pvc
set
pvc.vcstock = [{Level_LessOrderBook}]
where
pvc.vcsku='[{ItemNumber}]'
I formatted everything in exactly the same way as the rest of your existing script. Are you sure that the column prodvisible exists, is spelled correctly, and takes a numeric or bit value?