Page 1 of 1

Quick WHERE clause help!..

Posted: Mon Jan 23, 2006 6:11 pm
by seodevhead
I have a form that returns an array of primary key values for records in a DB that needs updated. Instead of having an update query in a foreach loop for each of the array values... I was thinking it would be faster to string together all the pk values and put it in a single UPDATE query.

My question is this. How do I word the WHERE clause in the UPDATE query to update all these seperate primary key values. For instance.

I want to update all records with primary keys of:

1, 3, 6, 14, 23, 39

I want to write a query like this:

Code: Select all

$query = "UPDATE table SET columns='blah' WHERE pk_col=1 AND pk_col=3 AND pk_col = 6... and so on.
Is this correct or is the syntax different for what I want to do? Thanks!!!

Re: Quick WHERE clause help!..

Posted: Mon Jan 23, 2006 7:16 pm
by raghavan20
seodevhead wrote:

Code: Select all

$query = "UPDATE table SET columns='blah' WHERE pk_col=1 AND pk_col=3 AND pk_col = 6... and so on.
That should have been an OR instead of AND

Easier way to do it is...

Code: Select all

$query = "UPDATE table SET columns='blah' WHERE pk_col in (1, 3, 5, 9, 23,..);