How to query a MYSQL database for all values within an array
I have a MYSQL database with with a users and a activity table. The user table containing user info and the activity table containing messages about a users activity on this site (similar to facebooks news feed).
Within the users table i have a stored an array of friends which is simply a commas separated string. (friend_list)
I am trying to select using mysql joins all the messages for all the friends in the friends list and also the current user and then order them by the activity timestamp.
I was thinking something like this...
$query=mysql_query("SELECT * FROM users,activity WHERE users.user=activ开发者_Python百科ity.user OR //### ACTIVITY.USER IS EQUAL TO ONE OF THE USERS.FRIEND_LIST ARRAY VALUES ### ORDER BY activity.timestamp")or die(mysql_error());
Its a tough one to describe. I hope you get it.
I dont know the syntax to compare activity.user against an array of values (users.friend_list)
Its probably very simple when you know how. Any ideas on this would be fantastic.
Fix your database schema. MySQL has no notion of "array of values". You should use tables, columns and rows to describe your data and the relationships between your entities.
You could just create a friends
table where you can store the IDs of two people being friends. Then it's just a matter of doing a join.
CREATE TABLE friends (
user1 INTEGER NOT NULL,
user2 INTEGER NOT NULL,
PRIMARY KEY (user1, user2)
);
精彩评论