Jump to content

Issues selecting multiple rows


ClemenzoDK

Recommended Posts

Hello, I have some issues selecting multiple rows, and yes I'm new to both MySQL and PHP. The query is as following:

 

SELECT *, CONCAT(locations.zip_code, ' ', locations.city) as location from locations, locations_associations where locations.zip_code = locations_associations.zip_code AND client_id = 1;

 

The query works just fine, but the thing is that I have multiple client_id's stored in a PHP array which I also need to know the location of. My logic tells me that the easiest way to insert multiple client_id's into the query, would be to implode the array and then insert each client_id into the query including a seperator of some sort. The alternative would be to create an loop that inserted a client_id one at a time, which seems like a long way around contra imploding. Anyway, is it possible in this case to put in multiple client_id's in the query, and how should they be seperated, I've tried with a comma like this:

 

SELECT *, CONCAT(locations.zip_code, ' ', locations.city) as location from locations, locations_associations where locations.zip_code = locations_associations.zip_code AND client_id = 1, 2, 3, 4;

 

Which gives me an error.

 

Thanks in advance.

 

Btw, I'm running MySQL 5.5.8 on my local machine.

Link to comment
Share on other sites

there is a bult in function in mysql that allows you to pass in an array as a parameter:

$array = (1,2,3,4);
SELECT *, CONCAT(locations.zip_code, ' ', locations.city) as location from locations, locations_associations where locations.zip_code = locations_associations.zip_code AND client_id IN ($array);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.