Crisp
02-03-2010, 04:41 AM
Hey,
I want to have an ID like: 1,2,3,4,5 and update WHERE `id`='1,2,3,4 (Whatever the ids are above)
Does anyone know how I can do this?
Much appreciated.
Thanks!
Len Whistler
02-03-2010, 05:04 AM
Why would you want to?
------------------------------------
Crisp
02-03-2010, 05:20 AM
That way I don't have to have a query in a while loop.
Because I'm getting the IDs in a while loop, imploding them with commas and now I want to update them.
Thanks.
Dormilich
02-03-2010, 05:25 AM
as a result of that, you have to use a while loop, to get each result line …
Coyote6
02-03-2010, 05:32 AM
Simple... Use the IN () context same as a whole bunch of OR statements. If your query is php just use this.
$q = "UPDATE table_1 SET some_field='some_value' WHERE id IN (1,2,3,4)";
If in a procedure you have to do it slightly different.
DELIMITER $$
CREATE PROCEDURE `update_fields` (IN in_ids TEXT)
BEGIN
SET @q = CONCAT ("DATE table_1 SET some_field='some_value' WHERE id IN (", in_ids, ")");
PREPARE STMT FROM @q;
EXECUTE STMT;
END$$
DELIMITER ;
// Use a pdo prepare and execute.
$q = "CALL update_fields (:ids)";
$p = array (':ids'=>'1,2,3,4');
You don't mention what RDBMS you're working with, can we assume MySQL? If so, you can use the MySQL IN clause, e.g.
$list= array(1,2,3,4,5);
$sql= 'UPDATE sometable SET somefield=1 WHERE id IN (' .join(",",$list). ')';
// WHERE IN (1,2,3,4,5)
Is this what you're looking for?
Coyote6
02-03-2010, 05:34 AM
Haha bdl... I got my answer in first! lol ;)
Funny cause I hit post and it said there was a new post already.
Crisp
02-03-2010, 05:35 AM
Yep that's it.
Thanks guys!
Haha bdl... I got my answer in first! lol ;)
Funny cause I hit post and it said there was a new post already.
I thought it was bizarre how close our code was... `somefield`, `some_field`... weird.
Coyote6
02-03-2010, 06:22 AM
lol ROFL haha I didn't see that! WOW!