update multiple records in mysql as a single query - mysql

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)

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)

updating record based on set if condition in gorm

I am trying to update the records in my Go project when trying to do so there is some need to update based on the conditions. I have figured out a way in mysql but that query need to be converted into gorm. Heere is the mysql query:
update table_name SET field_name1 = IF (field_name2 = 2, 1 , 0) where field_name3 = 6;
I have done this using
query := fmt.Sprintf(`UPDATE table_name SET field_name1 = IF (field_name2 = "%s", 1 , 0) where field_name3 = "%s"`, field_value1, field_value2)
s.DB.Exec(query)

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;

How to updated all row's field of one table from data of another table with one time query?

I have added a new field in data_entry table and want to insert on that the value of employee_write_task table's emp_id based on employee_write_task.task_id = data_entry.id and employee_write_task.empType_id = 1. 'here 1 is type of task for data_entry'
I have tried something like this:
INSERT INTO data_entry (emp_id)
SELECT emp_id FROM employee_write_task
WHERE employee_write_task.task_id = data_entry.id AND employee_write_task.empType_id = 1
but it doesn't seems to work. What am I missing?
I have attached tables and it's attributes below for references.
UPDATE data_entry
LEFT JOIN employee_write_task
ON data_entry.id = employee_write_task.task_id
SET data_entry.emp_id = employee_write_task.emp_id
WHERE employee_write_task.empType_id = 1;
I think you should be using update statement instead of insert. Try like follows.
UPDATE data_entry set emp_id =
(SELECT emp_id FROM employee_write_task
WHERE employee_write_task.task_id = data_entry.id AND employee_write_task.empType_id = 1)
This query may fail if the below select retrieve more than 1 value.
SELECT emp_id FROM employee_write_task
WHERE employee_write_task.task_id = data_entry.id
AND employee_write_task.empType_id = 1
In such case, you should add few more conditions to uniquely term the record.
Regards.
update data_entry d,employee_write_task e
set d.emp_id=e.emp_id
where d.id=e.task_id AND e.empType_id = 1;

Update multiple row (same column) with multiple value

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."';