Forum Moderators: open

Message Too Old, No Replies

SELECTing multiple rows with specific IDs

Can this be done with a single query?

         

createErrorMsg

12:52 pm on Nov 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



An application I'm putting together involves the user selecting multiple items from a page full of checkboxes. Each checkbox correlates to an item id in a mySQL table. On submitting those checkboxes, the script (PHP) pulls the ids from the form and queries the DB to get the details about all the items selected. Common stuff.

What I can't figure out is whether or not I can query the database once with all the IDs, retrieving all the data in one swoop, then parsing it up from there, or if I have to run a seperate query for each item. It's really not that big a deal if it has to be one at a time, but obviously I'd rather err on the side of efficiency if possible.

Say, for example, my user has selected the following ids:

id=2
id=16
id=93
id=102

Can anyone elighten me about syntax I can use in my SELECT query to retrieve all the corresponding rows in one query? Is this even possible?

Thanks in advance,
cEM

py9jmas

1:23 pm on Nov 12, 2005 (gmt 0)

10+ Year Member



SELECT * FROM table WHERE id IN (2, 16, 93,102);
should do the job.

createErrorMsg

6:42 pm on Nov 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perfect. Thank you so much.