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)
Related
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)
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;
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."';
Update myTable SET field = 1 (if field = 0 or if field is null) where myid = 12345;
Update myTable SET field = 0 (if field = 1) where myid = 12345;
What is the best way to transform this Pseudocode in proper SQL for Oracle and MySQL?
You could simply use the modulo like this:
UPDATE myTable SET field = (field + 1) % 2 WHERE myId = 12345;
Due to the lack of a real boolean in both DBMS you need a case statement:
update myTable
set the_column = case when the_column = 1 then 0 else 1 end
where myId = 12345;
This assumes that the column never has different values than 0 and 1
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.