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;
Related
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)
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)
files_imported
--------
ID (PK)
SPN
FILE_ID
LISTPRICE
ACTION
I need to search and UPDATE TableA and SET ACTION='A' WHERE the value of PRICE in a row with a given SPN is not equal to the value of PRICE found for a different FILE_ID and matching SPN.
I can do it with PHP by breaking the task into smaller queries but i'd rather have a clean solution with MySQL.
This is how far I've gotten:
UPDATE files_imported fi1
JOIN files_imported fi2
ON fi1.SPN = fi2.SPN
SET ACTION = 'M'
WHERE fi1.file_id = 980987987
AND fi1.listprice <> fi2.listprice
UPDATE files_imported fi1, files_imported fi2
SET f11.ACTION = 'M'
WHERE fi1.file_id = 980987987
AND fi1.SPN = fi2.SPN /* same SPN*/
AND fi1.listprice <> fi2.listprice /* different price */
AND fi1.file_id <> fi2.file_id /* different file */
I have the following sql that I run in C:
snprintf(sql, 200, "update rec set name = (select name from pers where id = %d )
where id = %d",rec_id , emp_id );
mysql_query(conn, sql) returns a successful result but it's putting 1 in the "rec" table in the "name" field instead of the name, but when I printf the output and use it in MySQL it's working fine.
update rec set name = (select name from pers where id = 104 ) where id = 43
Is there something wrong with my sprintf? Or something has to be added?
I also tried static sql command like this
snprintf(sql,"update rec set name = (select name from pers where id = 104 ) where id = 43");
and it also put 1 in the rec.name
Is that due to count of record returned by the sub query? Can you verify by putting a condition which returns e.g. 2 records so that the name is set to 2? if this is the reason then (though less performing approach) try splitting the queries and see if it works this time.
I have a flag in my database called published, I set this to 1 for a published row. My question is , is there a way to set all other rows to 0 and set a particular row to 1 with just one query.
At the moment im using:
$db->query("UPDATE my_table SET published = '0'");
$db->query("UPDATE my_table SET published = '1' WHERE id = '$id'");
UPDATE my_table SET published = IF (id = $id,1,0);
Use a CASE Statement
UPDATE my_table
SET published = CASE
WHEN id = '$id' THEN 1
ELSE 0 END
In MySQL, there's no boolean type (conditions return an integer), so this works too :
UPDATE my_table
SET published = (id = $id);
id = $id returns 0 if $id is different than id, else 1.