mysql等于多个,MySQL选择等于多个值的地方

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?

解决方案

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.

Why do you have two databases? Do you mean two tables?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值