Make 7 SQL Query Like This Into Single Query, Is It Possible? - mysql

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.

Related

How to use 2 different condition substitute for each other in the same SELECT

I'm having this issue
Now, I'm having this table
Code
FirstNo
LastNo
Date_input
ABC1234
12
34
2022/06/06
ABCD5678
56
78
2022/07/07
ABCD9111
91
11
2022/07/07
DEF1234
12
34
2022/06/06
DEF5678
56
78
2022/07/07
Then, I want to return 2 random number (these 2 number is from 00 to 99) which will be taken from front-end. Lets call them Random1 and Random2.
Now, when the query running, it will run from record 1 --> final
If whenever the FirstNo = Random1, then the next record will take the condition of FirstNo = Random2. Then, at the next record, it will return to get the condition of FirstNo = Random1.
For example, Random1 = 56, Random2 = 91, and the table as above
First record: FirstNo != 56, move to the next row.
Second record: FirstNo = 56.
Third record: (Because of at the previous record, FirstNo equal Random1 (56), this record will get Random2 instead) FirstNo = 91.
Fourth record: Back to Random1, .....
So, I'm figuring out something like:
SELECT * FROM CODE_DEX WHERE FirstNo = #Random1
SELECT * FROM CODE_DEX WHERE FirstNo = #Random2
I haven't have any idea how to joining these 2. I can create a column ISTRUE or something to Declare and Set in SQL, but I don't think I can use IF in the middle of the sql query.
Thank you so much
Based on stated requirement
If the prior row value for FirstNo ordered by date is Random1 then return random2 for FirstNo
You would do that like this in SQL
SELECT Code,
CASE WHEN LAG(FirstNo,1) OVER (ORDER BY Date_Input) = Random1
THEN Random2
ELSE FirstNo END AS FirstNo,
LastNo,
Date_input
FROM sometableyoudidnotname
ORDER BY Date_input
You can select:
next value, with the LEAD window function
previous value, with the LAG window function
then filter out all rows which don't have "Random1" and "Random2" values in consecutive rows:
SET #Random1 = 56;
SET #Random2 = 91;
WITH cte AS (
SELECT *,
LAG(FirstNo) OVER(ORDER BY Code, Date_input) AS PrevNo,
LEAD(FirstNo) OVER(ORDER BY Code, Date_input) AS NextNo
FROM tab
)
SELECT Code,
FirstNo,
LastNo,
Date_input
FROM cte
WHERE (FirstNo = #Random1 AND NextNo = #Random2)
OR (FirstNo = #Random2 AND PrevNo = #Random1)
Check the demo here.

MySQL trigger to change products price status to inactive after a new price list become active

I need to create a before update trigger to change the status of "old" product prices' after the new one becomes 'active'
price list table:
id product_id supplier list_price_uom start_date price_status
1 101761 ABC 16.26 2021-02-01 Active
2 101761 DEF 19.5 2021-07-01 Active
3 1086 ABC 32.52 2021-02-01 Active
4 1087 ABC 32.52 2021-02-01 Active
5 AWBWHL ABC 82.23 2021-02-01 Active
6 101761 ABC 19.26 2021-08-30 Scheduled
7 1086 ABC 37.52 2021-08-30 Scheduled
8 1087 ABC 34.52 2021-08-30 Scheduled
9 AWBWHL ABC 85.23 2021-08-30 Scheduled
I have an event that updates the status to 'Active' when start_date = curdate()
CREATE EVENT "change_status"
ON SCHEDULE EVERY 1 DAY STARTS
'2021-08-26 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO UPDATE
price_list_test SET status = 'Active' WHERE status = 'Scheduled'
AND start_date = curdate()
all I need is to change all 'active' rows to 'inactive' when the same SKU (from the same supplier) gets updated by the event.
I have tried different variations of:
BEFORE UPDATE ON "price_list_test" FOR EACH ROW
BEGIN IF (new.product_id = old.product_id) and (new.supplier = old.supplier) and (old.start_date < CURDATE()) THEN SET old.price_status = 'Inactive';
end if ;
END
this one gives me the error "updating OLD row is not allowed in trigger:
removing the "old" gives me the error "unknown system variable 'price_status'"
tried this one:
BEFORE UPDATE ON "price_list_test" FOR EACH ROW BEGIN
UPDATE price_list_test SET price_status = 'Inactive' where new.product_id = old.product_id and old.start_date < CURDATE()
;
which gives me this error
"Error Code: 1442. Can't update table 'price_list_test' in stored function/trigger because it is already used by statement which invoked this stored function/trigger."
which makes sense and just ran out of ideas.
any help will be very much appreciated.
Perform everything in the event procedure:
CREATE EVENT change_status
ON SCHEDULE EVERY 1 DAY
STARTS '2021-08-26 00:00:00'
ON COMPLETION NOT PRESERVE
ENABLE
DO
UPDATE price_list_test t1
LEFT JOIN price_list_test t2 ON t1.SKU = t2.SKU /* replace with correct condition */
AND t2.status = 'Active'
SET t1.status = 'Active',
t2.status = 'Inactive'
WHERE t1.status = 'Scheduled'
AND start_date = CURRENT_DATE;
None additional operation/code needed in this case.

Mysql update query with IF statement

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

How to loop a MYSQL query / maybe in a function?

What I've got so far is a code which updates one row as based on the IF conditions - but it
It is important that it is reliable and atomic (so with transaction).
It should loop/repeat itself based on #q which is quantity - however it can be in a MYSQL function rather than one fixed variable. How can I do that? I've added how the table should look like in the end
SET #q=1000;
SET #p=5.00;
SET #email='test#test.com';
update 1detail
SET quantity =
if((#q := quantity - #q) >= 0, #q, 0)
WHERE price>=#p ORDER BY datetime DESC LIMIT 1;
SET #q = if(#q <0,#q-#q-#q, #q);
UPDATE 1detail
SET quantity =
if(#q > 0, #q,quantity),
email=
if(#q > 0, #email, email)
WHERE price>=#p ORDER BY datetime DESC LIMIT 1;
The table at the beginning
quantity price email datetime
---------------------------------------------------
800 5.00 test1#test.com oldest
50 5.00 test2#test.com 2nd oldest
100 10.00 test3#test.com 3rd oldest (ignore in processing because #p < price)
How it should looks like after looping
quantity price email datetime
-----------------------------------------------------------------
0 5.00 test1#test.com oldest
150* 5.00 test#test.com (changed to #email) 2nd oldest (now newest)
100 10.00 test3#test.com 3rd oldest (ignored)
*this has changed as the oldest only had 800 and the 2nd oldest just had 50
So #q= 1000 (#q) - 800 (oldest) = 200
then #q = 200 (#q) - 50 (2nd oldest)
--> #q = 150
this updates the 2nd oldest row
A small loop can be achieved like this :
declare #counter int
set #counter=0
gohere:
set #counter=#counter+1
if #counter<10
BEGIN
select #counter
GOTO gohere;
END

How to run more SQL updates in one SQL command?

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'