Quick WHERE clause help!..

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Quick WHERE clause help!..

Post 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!!!
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Re: Quick WHERE clause help!..

Post 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,..);
Post Reply