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
我的问题是,如果一个用户跟踪多个用户,我就从这个用户跟踪的用户的用户id中进行回显,得到443344330
When I separate each id with commans, I get:
当我和突击队员分开每个id时,我得到:
44,44,33,44,33,0,
so let's give that a variable of $user_ids
;
我们给它一个变量$user_id;
$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.
但是它所做的就是显示第一个用户id 44的记录。
How can I retrieve all of the records for all the users?
如何检索所有用户的所有记录?
5 个解决方案
#1
20
Maybe you want to use IN
也许你想用它。
SELECT * FROM posts WHERE userid IN ($user_ids)
#2
19
The query should be:
查询应该是:
SELECT * FROM posts WHERE userid IN (44,44,33,44,33,0)
However, you may have to rethink your data model and make sure it is normalized, so that you can express this construction directly in the databse without echoing into a comma-separated string.
但是,您可能需要重新考虑您的数据模型,并确保它是规范化的,这样您就可以直接在databse中表达这个构造,而不需要响应一个逗号分隔的字符串。
Why do you have two databases? Do you mean two tables?
为什么有两个数据库?你是说两张桌子吗?
#3
1
Assuming you have two tabels, not databases, and that the table (lets call it "friends") which describes who is following who is like
假设您有两个表,而不是数据库,并且这个表(我们称它为“friends”)描述了谁在跟踪谁是谁
table friends(
userid
friendid
)
then query to get posts posted by X's friends would be
然后查询得到X的朋友发布的帖子。
SELECT
p.*
FROM posts p
JOIN friends f ON(p.userid = f.friendid)
WHERE f.userid = X
#4
0
$get_posts = mysql_query("SELECT * FROM posts WHERE userid in '($user_ids)'");
#5
0
$var= str_replace(","," or userid = ","userid =$your_data_from_db");
your myqsl_query = SELECT * FROM posts where $var
myqsl_query =从$var的帖子中选择*
#1
20
Maybe you want to use IN
也许你想用它。
SELECT * FROM posts WHERE userid IN ($user_ids)
#2
19
The query should be:
查询应该是:
SELECT * FROM posts WHERE userid IN (44,44,33,44,33,0)
However, you may have to rethink your data model and make sure it is normalized, so that you can express this construction directly in the databse without echoing into a comma-separated string.
但是,您可能需要重新考虑您的数据模型,并确保它是规范化的,这样您就可以直接在databse中表达这个构造,而不需要响应一个逗号分隔的字符串。
Why do you have two databases? Do you mean two tables?
为什么有两个数据库?你是说两张桌子吗?
#3
1
Assuming you have two tabels, not databases, and that the table (lets call it "friends") which describes who is following who is like
假设您有两个表,而不是数据库,并且这个表(我们称它为“friends”)描述了谁在跟踪谁是谁
table friends(
userid
friendid
)
then query to get posts posted by X's friends would be
然后查询得到X的朋友发布的帖子。
SELECT
p.*
FROM posts p
JOIN friends f ON(p.userid = f.friendid)
WHERE f.userid = X
#4
0
$get_posts = mysql_query("SELECT * FROM posts WHERE userid in '($user_ids)'");
#5
0
$var= str_replace(","," or userid = ","userid =$your_data_from_db");
your myqsl_query = SELECT * FROM posts where $var
myqsl_query =从$var的帖子中选择*