Update multiple row (same column) with multiple value - mysql

I have
Array ( [406] => 1 [407] => 3 [408] => 2 [409] => 7 [410] => 1 )
run as mysql query
UPDATE counter SET total = 1 WHERE id = 406;
UPDATE counter SET total = 3 WHERE id = 407;
UPDATE counter SET total = 2 WHERE id = 408;
UPDATE counter SET total = 7 WHERE id = 409;
UPDATE counter SET total = 1 WHERE id = 410;
I can only optimized query above by grouping same total value as below:
UPDATE counter
SET total = 1
WHERE name IN (406, 410);
Is there any way to optimize it better, rather than execute (loop) the update query one by one.

You need this:
UPDATE counter SET total = CASE
WHEN id = 406 THEN 1
WHEN id = 407 THEN 3
WHEN id = 408 THEN 2
WHEN id = 409 THEN 7
WHEN id = 410 THEN 1
END

you can use key value pair to update like below
UPDATE counter SET total = '".$value."' WHERE id = '".$key."';

Related

Update SQL multiple row value in single query with multiple column combination

enter image description here
I have table with these datas, after do some operations I want to update flag value from 0 to 1 based on the two columns value code and id
update table set flag = 1 where code = 'ABC' and id = 10000
update table set flag = 1 where code = 'DEF' and id = 10001
update table set flag = 1 where code = 'GHI' and id = 10002
update table set flag = 1 where code = 'ABC' and id = 10001
I can do like this with foreach But I want to update using single query
How can I achieve this?
this should work
UPDATE table SET flat = 1 WHERE (code = 'ABC' and id = 10000) OR (code = 'DEF' and id = 10001) OR (code = 'GHI' and id = 10002)

MySQL UNION SELECT FOLLOWING NEXT COLUM ID

I'm creating a table to store rules for another selects and it has the following design:
id
column
operator
value
next_rule_id
next_operator
1
mail
contains
#gmail.com
2
OR
2
mail
contains
#hotmail.com
5
AND
3
city
equals
NY
5
AND
4
mail
contains
jolie#
3
AND
5
city
equals
NY
4
null
6
user_id
greater
10
null
null
I need to select 1 rule id and return all rows based on next_rule_id
Here are some examples:
selecting rule id = 1 would return
1 mail contains #gmail.com 2 OR
2 mail contains #hotmail.com 5 AND
5 city equals NY 4 null
selecting rule id = 4 would return
4 mail contains jolie# 3 AND
3 city equals NY 5 AND
5 city equals NY 4 null
selecting rule id = 6 would return
6 user_id greater 10 null null
SOLUTION 1
I've managed to do that with a temporary table and one loop. But I think there is better approaches to do that.
If anyone knows how to do that in a better way I would appreciate your help.
SOLUTION 2
I created this procedure that uses a loop too.
BEGIN
SET #id = 1; -- hardcoded first id
SET #rules = '';
SET #next_id = NULL;
loop1: LOOP
SET #count = #count + 1;
SELECT next_rule_id INTO #next_id FROM filter_rules WHERE id = #id;
SET #rules = CONCAT(#rules, ',', #id);
SET #id = #next_id;
IF #id IS NULL THEN
LEAVE loop1;
END IF;
END LOOP loop1;
SELECT * FROM filter_rules WHERE FIND_IN_SET(filter_rules.id, #rules);
END

update multiple records in mysql as a single query

Is there a way to combine these sql statement
UPDATE foreign_users_to_be_mentioned SET is_used = 1 WHERE id = 1
UPDATE foreign_users_to_be_mentioned SET is_used = 1 WHERE id = 2
into a single query ?
Use IN:
UPDATE foreign_users_to_be_mentioned SET is_used = 1 WHERE id IN (1, 2)

Set to Boolean value based on IN clause

How can the following be performed in a single query?
UPDATE clients SET online=0 WHERE id NOT IN(4,5,8,10,12) AND id>=2 AND id<=15 AND parentId=123;
UPDATE clients SET online=1 WHERE id IN(4,5,8,10,12) AND id>=2 AND id<=15 AND parentId=123;
You can use CASE .. WHEN statement:
UPDATE clients
SET online = CASE WHEN id IN(4,5,8,10,12)
THEN 1
ELSE 0
END
WHERE
id BETWEEN 2 AND 15 AND parentId = 123;
IN(..) is a Logical/Comparison function. So you can do the following (in MySQL only) as well:
UPDATE clients
SET online = (id IN(4,5,8,10,12))
WHERE
id BETWEEN 2 AND 15 AND parentId = 123;

Comparing mysql tables and update a value

I want to compare real results and predictions from 2 similar tables on mysql.
real
id | data1| data2 |
user
id | data1| data2 | points
ranking
id | user| total points
I want to do the following:
if (real.data1 = user.data1) AND (real.data2 = user.data2)
update user set points=8 where id=1
else if(real.data1 > user.data1) AND (real.data2 > user.data2)
update user set points=4 where id=1
else if (real.data1 = real.data2) AND (user.data1 = user.data2)
update user set points=4 where id=1
else if (real.data1 < user.data1) AND (real.data2 < user.data2)
update user set points=4 where id=1
else
update user set points=0 where id=1
sum all values from points and update ranking table
Is it possible?
I believe the below will work for the first half of your question, but I have not tested it:
UPDATE `user` u
INNER JOIN `real` r ON (u.id = r.id)
SET u.points = IF(r.data1 = u.data1 and r.data2 = u.data2,
8,
IF(r.data1 > u.data1 and r.data2 > u.data2,
4,
IF(r.data1 = r.data2 and u.data1 = u.data2,
4,
IF(r.data1 < u.data1 and r.data2 < u.data2,
4,
0)
)
)
)
See the MySQL docs concerning the IF statement if this doesn't make sense.