MySQL select where equal to multiple values

Viewed 75740

I have two databases, where I store who is following who and another which stores the posts the user makes.

I want to select all of the people a user is following from the following database and echo out the usernames of those who that user is following and query the posts database for posts of that user.

My problem is what if a user is following multiple users, I echoed out of the user id's of the people this user is following and I get 44443344330

When I separate each id with commans, I get:

44,44,33,44,33,0, 

so let's give that a variable of $user_ids;

$user_ids = "44,44,33,44,33,0, ";

the query:

$get_posts = mysql_query("SELECT * FROM posts WHERE userid = '$user_ids'");

but all it does is show the records of the first user id, 44.

How can I retrieve all of the records for all the users?

6 Answers

Given that you have two tables, not two databases, the easiest way to match multiple values for a specific column would be the following:

SELECT * FROM posts WHERE userid IN (44,44,33,44,33,0)

*A small point that I ran into when doing this. If you are matching to a column that is of type VARCHAR(n), be sure to wrap your matching values in 'single quotes', not "double quotes"

e.g:

SELECT * FROM posts WHERE name IN ('foo','bar','alpha','beta')
Related