MySQL选择等于多个值的地方 [英] MySQL select where equal to multiple values

查看:647
本文介绍了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.

我的问题是,如果一个用户关注多个用户, 我回避了该用户关注的人的用户ID,我得到了 44443344330

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时,我得到:

When I separate each id with commans, I get:

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

所以我们给它一个变量$user_ids;

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

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

查询:

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

但是它所做的只是显示第一个用户ID(44)的记录.

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?

推荐答案

查询应为:

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?

这篇关于MySQL选择等于多个值的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆